Vim; Executing another application by :MyCommand

0

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.

Kaveh Shahbazian

Posted 2013-03-18T09:17:54.107

Reputation: 127

Answers

3

: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

Ingo Karkat

Posted 2013-03-18T09:17:54.107

Reputation: 19 513

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 – 2013-03-18T09:36:09.207

You still need to define a custom command: :command Goinstall !go install if you don't want to use :make. – Ingo Karkat – 2013-03-18T09:43:26.923

I did and it worked. Thanks :) My problem is the execution path. – Kaveh Shahbazian – 2013-03-18T10:24:40.760

I've added that to my answer. – Ingo Karkat – 2013-03-18T10:27:35.743

Thanks; It really helped. Now if I want to not leave Vim how can I see the output inside Vim itself? – Kaveh Shahbazian – 2013-03-18T10:39:25.220

1Then :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 – 2013-03-18T11:22:44.813

Go has vim plugin in it's distribution but not a vimfiles\compiler\go.vim so: I am learning how to write vim scripts! – Kaveh Shahbazian – 2013-03-18T12:20:13.707