How to run go lang code directly from vim?

huangapple go评论104阅读模式
英文:

How to run go lang code directly from vim?

问题

如何直接从vim编辑器中运行Go语言代码?例如,当前正在编辑的文件是array.go。我应该使用什么样的命令在vim中运行这段代码?

英文:

How to run go lang code directly from vim editor ?

e.g. currently edited file is array.go.
What kind of commands should I use to run this code in vim?

答案1

得分: 6

好的,以下是翻译好的内容:

好的,经过一些尝试,这个命令可以工作:!go run %

英文:

ok so after some trials this works: !go run %

答案2

得分: 3

假设您已经正确设置了环境变量;您可以使用这个 _gvimrc 文件(在Windows上,它应该位于 C:\Users\用户名;在Linux上,我不清楚):

set guifont=Lucida_Console:h11
colorscheme dejavu
set tabstop=4

filetype plugin on
filetype plugin indent on
syntax on

" causes vim opens maximized in windows (@least)
au GUIEnter * simalt ~x

set autochdir
set number
set nobackup
" set nowritebackup

" this made my vim life (as a begginer at least) much happier!
" thanks to @ http://vim.wikia.com/wiki/Display_output_of_shell_commands_in_new_window bottom of the page
function! s:ExecuteInShell(command, bang)
    let _ = a:bang != '' ? s:_ : a:command == '' ? '' : join(map(split(a:command), 'expand(v:val)'))

    if (_ != '')
        let s:_ = _
        let bufnr = bufnr('%')
        let winnr = bufwinnr('^' . _ . '$')
        silent! execute  winnr < 0 ? 'belowright new ' . fnameescape(_) : winnr . 'wincmd w'
        setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile wrap number
        silent! :%d
        let message = 'Execute ' . _ . '...'
        call append(0, message)
        echo message
        silent! 2d | resize 1 | redraw
        silent! execute 'silent! %!' . _
        silent! execute 'resize ' . line('$')
        silent! execute 'syntax on'
        silent! execute 'autocmd BufUnload <buffer> execute bufwinnr(' . bufnr . ') . ''wincmd w'''
        silent! execute 'autocmd BufEnter <buffer> execute ''resize '' .  line(''$'')'
        silent! execute 'nnoremap <silent> <buffer> <CR> :call <SID>ExecuteInShell(''' . _ . ''', '''')<CR>'
        silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . _ . ''', '''')<CR>'
        silent! execute 'nnoremap <silent> <buffer> <LocalLeader>g :execute bufwinnr(' . bufnr . ') . ''wincmd w''<CR>'
        nnoremap <silent> <buffer> <C-W>_ :execute 'resize ' . line('$')<CR>
        silent! syntax on
    endif
endfunction

command! -complete=shellcmd -nargs=* -bang Shell call s:ExecuteInShell('<q-args>', '<bang>')
cabbrev shell Shell

" my additional tools
command! -complete=shellcmd -nargs=* -bang Gor call s:ExecuteInShell('go run %', '<bang>')
command! -complete=shellcmd -nargs=* -bang Gon call s:ExecuteInShell('go install', '<bang>')
command! -complete=shellcmd -nargs=* -bang Gob call s:ExecuteInShell('go build', '<bang>')
command! -complete=shellcmd -nargs=* -bang Got call s:ExecuteInShell('go test -v', '<bang>')

:map <F5>  :Gor<CR>
:map <F6>  :Gob<CR>
:map <F7>  :Gon<CR>
:map <F9>  :Got<CR>
:map <F10> :Fmt<CR>:w<CR>
:map <F12> :q<CR>

cabbrev fmt Fmt

:set encoding=utf-8
:set fileencodings=utf-8

现在当您按下 F5 键时,它将运行go(go run file.go)并在另一个文档中显示输出(您可以使用 :q 关闭另一个文档)。其他命令包括:F6 编译,F7 安装,F9 测试和(我最喜欢的)F10fmt+:w

顺便说一句,dejavu 是我最喜欢的配色方案(代码来自gorilla/mux):

How to run go lang code directly from vim?

英文:

Assuming that you've set environment variables properly; you can use this _gvimrc file (on Windows, it should be at C:\Users\UserName; on Linux, I do not know):

set guifont=Lucida_Console:h11
colorscheme dejavu
set tabstop=4

filetype plugin on
filetype plugin indent on
syntax on

&quot; causes vim opens maximized in windows (@least)
au GUIEnter * simalt ~x

set autochdir
set number
set nobackup
&quot; set nowritebackup

&quot; this made my vim life (as a begginer at least) much happier!
&quot; thanks to @ http://vim.wikia.com/wiki/Display_output_of_shell_commands_in_new_window bottom of the page
function! s:ExecuteInShell(command, bang)
	let _ = a:bang != &#39;&#39; ? s:_ : a:command == &#39;&#39; ? &#39;&#39; : join(map(split(a:command), &#39;expand(v:val)&#39;))

	if (_ != &#39;&#39;)
		let s:_ = _
		let bufnr = bufnr(&#39;%&#39;)
		let winnr = bufwinnr(&#39;^&#39; . _ . &#39;$&#39;)
		silent! execute  winnr &lt; 0 ? &#39;belowright new &#39; . fnameescape(_) : winnr . &#39;wincmd w&#39;
		setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile wrap number
		silent! :%d
		let message = &#39;Execute &#39; . _ . &#39;...&#39;
		call append(0, message)
		echo message
		silent! 2d | resize 1 | redraw
		silent! execute &#39;silent! %!&#39;. _
		silent! execute &#39;resize &#39; . line(&#39;$&#39;)
		silent! execute &#39;syntax on&#39;
		silent! execute &#39;autocmd BufUnload &lt;buffer&gt; execute bufwinnr(&#39; . bufnr . &#39;) . &#39;&#39;wincmd w&#39;&#39;&#39;
		silent! execute &#39;autocmd BufEnter &lt;buffer&gt; execute &#39;&#39;resize &#39;&#39; .  line(&#39;&#39;$&#39;&#39;)&#39;
		silent! execute &#39;nnoremap &lt;silent&gt; &lt;buffer&gt; &lt;CR&gt; :call &lt;SID&gt;ExecuteInShell(&#39;&#39;&#39; . _ . &#39;&#39;&#39;, &#39;&#39;&#39;&#39;)&lt;CR&gt;&#39;
		silent! execute &#39;nnoremap &lt;silent&gt; &lt;buffer&gt; &lt;LocalLeader&gt;r :call &lt;SID&gt;ExecuteInShell(&#39;&#39;&#39; . _ . &#39;&#39;&#39;, &#39;&#39;&#39;&#39;)&lt;CR&gt;&#39;
		silent! execute &#39;nnoremap &lt;silent&gt; &lt;buffer&gt; &lt;LocalLeader&gt;g :execute bufwinnr(&#39; . bufnr . &#39;) . &#39;&#39;wincmd w&#39;&#39;&lt;CR&gt;&#39;
		nnoremap &lt;silent&gt; &lt;buffer&gt; &lt;C-W&gt;_ :execute &#39;resize &#39; . line(&#39;$&#39;)&lt;CR&gt;
		silent! syntax on
	endif
endfunction

command! -complete=shellcmd -nargs=* -bang Shell call s:ExecuteInShell(&lt;q-args&gt;, &#39;&lt;bang&gt;&#39;)
cabbrev shell Shell

&quot; my additional tools
command! -complete=shellcmd -nargs=* -bang Gor call s:ExecuteInShell(&#39;go run %&#39;, &#39;&lt;bang&gt;&#39;)
command! -complete=shellcmd -nargs=* -bang Gon call s:ExecuteInShell(&#39;go install&#39;, &#39;&lt;bang&gt;&#39;)
command! -complete=shellcmd -nargs=* -bang Gob call s:ExecuteInShell(&#39;go build&#39;, &#39;&lt;bang&gt;&#39;)
command! -complete=shellcmd -nargs=* -bang Got call s:ExecuteInShell(&#39;go test -v&#39;, &#39;&lt;bang&gt;&#39;)

:map &lt;F5&gt;  :Gor&lt;CR&gt;
:map &lt;F6&gt;  :Gob&lt;CR&gt;
:map &lt;F7&gt;  :Gon&lt;CR&gt;
:map &lt;F9&gt;  :Got&lt;CR&gt;
:map &lt;F10&gt; :Fmt&lt;CR&gt;:w&lt;CR&gt;
:map &lt;F12&gt; :q&lt;CR&gt;

cabbrev fmt Fmt

:set encoding=utf-8
:set fileencodings=utf-8

Now when you press F5 it will run go (go run file.go) and shows the output in another document (you can :q to close the other document). Other commands are: F6 build, F7 install, F9 test and (my beloved one) F10 is fmt+:w.

BTW dejavu is my favorite color-scheme (code is from gorilla/mux):

How to run go lang code directly from vim?

答案3

得分: 0

你也可以在你的 ~/.vimrc 文件中映射一个按键,如下所示:

nnoremap gr :!go run %<CR>

这样你就可以在 vim 中轻松地输入 gr,它会执行相应的命令。

英文:

You can also map a key in your ~/.vimrc as this

nnoremap gr :!go run %&lt;CR&gt;

So you can easily type gr in your vim and it will execute.

答案4

得分: 0

将以下两行代码添加到你的vimrc文件中。

autocmd FileType go map <buffer> <F9> :w<CR>:exec '!go run' shellescape(@%, 1)<CR>

autocmd FileType go imap <buffer> <F9> <esc>:w<CR>:exec '!go run' shellescape(@%, 1)<CR>

这是受到类似Python问题的答案的启发。

英文:

add the following two lines to your vimrc.

autocmd FileType go map &lt;buffer&gt; &lt;F9&gt; :w&lt;CR&gt;:exec &#39;!go run&#39; shellescape(@%, 1)&lt;CR&gt;

autocmd FileType go imap &lt;buffer&gt; &lt;F9&gt; &lt;esc&gt;:w&lt;CR&gt;:exec &#39;!go run&#39; shellescape(@%, 1)&lt;CR&gt;

this is inspired by an answer to a similar python question

huangapple
  • 本文由 发表于 2013年10月27日 03:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/19610698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定