Reputation: 1168
I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.
Within my sublime setup, it runs go build
anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build
provides.
I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.
In short I want to run :GoBuild
upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?
Upvotes: 2
Views: 1691
Reputation: 12755
Create ~/.vim/after/ftplugin/go.vim
with:
autocmd BufWritePre *.go :GoBuild
Upvotes: 0
Reputation:
yes, use vim autocommand
to run :GoBuild
:
You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. The usual place to put autocommands is in your .vimrc or .exrc file.
Run the following command:
:autocmd BufWritePre *.go :GoBuild
Now each time you save your Go file with :w
it will run :GoBuild
too.
The event type is BufWritePre
, which means the event will be checked just before you write *.go
file.
BufWritePre
starting to write the whole buffer to a file
BufWritePost
after writing the whole buffer to a file
and:
When your .vimrc file is sourced twice, the autocommands will appear twice. To avoid this, put this command in your .vimrc file, before defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded") : let autocommands_loaded = 1 : au ... :endif
like this (put this at the end of your vim startup file):
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: autocmd BufWritePost *.go :GoBuild
:endif
ref:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
Upvotes: 2