duanli9591
duanli9591
2013-10-26 19:02

如何直接从vim运行go lang代码?

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtvjl64442 dtvjl64442 8年前

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

    点赞 评论 复制链接分享
  • dongpeng0127 dongpeng0127 8年前

    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
    
    " 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
    

    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):

    enter image description here

    点赞 评论 复制链接分享

为你推荐