4

如何go lang code直接从运行vim editor

例如,当前编辑的文件是array.go. 我应该使用什么样的命令在 vim 中运行这段代码?

4

4 回答 4

5

好的,经过一些试验后,这可行:!go run %

于 2013-10-26T19:11:04.273 回答
3

假设您已正确设置环境变量;你可以使用这个_gvimrc文件(在 Windows 上,它应该在C:\Users\UserName; 在 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

BTWdejavu是我最喜欢的配色方案(代码来自 gorilla/mux):

在此处输入图像描述

于 2013-11-02T16:11:38.257 回答
0

您也可以~/.vimrc像这样映射一个键

nnoremap gr :!go run %<CR>

所以你可以很容易地输入gr你的vim,它就会执行。

于 2021-05-23T04:48:52.663 回答
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 问题的答案的启发

于 2021-06-04T10:28:30.360 回答