Setting up Vim for Go

A more updated post on setting up VIM is here.

I’ve been playing around with the Go programming language recently and so I wanted to configure VIM to work better with it. The Go package comes with vim plugins for syntax highlighting, indentation and helper functions which is a good start, but I wanted to have some of the nice things that I set up previously for other languages that used more frequently such as being able to jump to functions, auto completions, etc. The Go community have made this dead simple and will only take a few minutes.

Auto-Completion
**
**While I’m not a big fan of IDEs, one feature I always do want is context aware auto-completion (e.g. type in a partial function name and get a list of possibilities). I remember using a Norton tool as far as the DOS days since I’m terrible at remembering the exact spelling of function names/variables, members, etc.

Gocode is a daemon that will provide this functionality. It is a daemon that is editor-agnostic so it can be used by VIM, Emacs, Sublime Text, etc. Setup is a one-liner:
go get -u github.com/nsf/gocodeThen use the vundle VIM plugin by adding this to your .vimrc:
bundle Blackrush/vim-gocode The plugin will automatically start the daemon if it is not already running or you can start/stop it manually.

I use the SuperTab plugin so that when I hit “Tab” that it brings up the autocomplete menu so I set this in my .vimrc:
let g:SuperTabDefaultCompletionType = “context”There you go! Auto-completion for Go in VIM.

CTags/Tagbar

I use ctags to to let VIM know how to jump to declarations (e.g. cursor on a function name and jump to the function definition). To add Go support to CTags, use the gotags package.
go get -u github.com/jstemmer/gotagsThe README.md even has the VIM configuration for using with the Tagbar plugin!

Misc
**
**I also added this to my .vimrc:

The first line will run the Go code formatter whenever the file is saved. The second line shows a tab as 4 spaces instead of 8 spaces that is the default for Go.

LazyHacker avatar
About LazyHacker
Just a boring guy.