Monday, January 7, 2013

Using vim's path to speed up your Go project

If you're working on a project in Go, and you're using vim, then you really need to check out path:
:help path
Here's a very quick tour.

Add this to you .vimrc:
set path +=/your/projects/gopath/src/**
** tells path to include all subdirectories. By adding your projects GOPATH entry it opens up access to two rather cool features that are useful in navigating your project.

:find

You can use :find to open a file instead of using :e. But :find will look in all of your project's directories to find the file. So rather than having to do:
:e /package/v10/another-dir/god.go
You just need:
:find god.go
You also get tab-completion for free - just in case you can't remember the file's name.

gf

gf is the useful little command that goes to the file under the cursor. Well providing you've set your path correcly you can navigate your project's imports with ease:
package main

import (
 "code.google.com/p/go.net/websocket"
)
By "gf"ing over the import vim will open the directory, giving you the list of files in that package.

Just don't dare navigate the directory using the arrow keys!

Update: 8th Jan 2012

On the subject of vim and Go it's worth remembering this little gem:
au BufWritePost *.go !gofmt -w %
Whenever a Go file is saved gofmt will be run against it.

No comments:

Post a Comment