Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to execute "go install" in current directory (where currently open document resides) with a command (Like :GoInstall).

How can I do that?

Note: I want to see the output of that command too.

I have added command Goin execute "go install" to _gvimrc and _vimrc (I am on Windows), but it does not work - says it's not an editor command - or gives E488 and I am not sure it is being executed in current directory.

UPDATE:

After some struggling to know Vim better and mostly by googling I ended up with this _gvimrc file which works perfectly (at least for me). It adds three commands Gon, Gob and Gor to run go install, go build and go run current_file.go and shows the result in another document (buffer) in Vim. Hope it helps somebody else who is a Vim beginner:

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

" 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

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

:map <F5> :Gor<CR>
:map <F6> :Gob<CR>
:map <F7> :Gon<CR>

Note: You have to set up at least GOROOT and GOPATH env-vars on your system.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

:execute is for built-in Ex commands, you want the :! command to execute an external command:

:!go install

It appears that the 'makeprg' option would be beneficial here, too. Compilation is such a frequent task that vi / Vim have a trigger mechanism built-in. If you

:set makeprg=go

you can trigger the build with :make install.


To change to the current directory, either use

:cd %:h

or (to always to this automatically):

:set autochdir
share|improve this answer
    
Thanks; I want to have this in _gvimrc. ! worked but it executes the command in Windows directory and shows no output: C:\Windows\system32\cmd.exe /c "go install" Hit any key to close this window... I prefer to see the result in vim itself. –  Kaveh Shahbazian Mar 18 '13 at 9:36
    
You still need to define a custom command: :command Goinstall !go install if you don't want to use :make. –  Ingo Karkat Mar 18 '13 at 9:43
    
I did and it worked. Thanks :) My problem is the execution path. –  Kaveh Shahbazian Mar 18 '13 at 10:24
    
I've added that to my answer. –  Ingo Karkat Mar 18 '13 at 10:27
1  
Then :make is the way to go; Vim will capture the output, parse it for errors (see 'errorformat'), and can show it in the quickfix list. But I wonder whether you need to invent all that on your own, try googling for a "Go language compiler plugin". –  Ingo Karkat Mar 18 '13 at 11:22
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.