Navigation between functions and types in vim-go

I’ve introduced a new tool called motion in my previous post (I recommend to read it before you continue) and explained new features (such as text-objects) that are implemented on top of it.

Text motion objects

Vim-go has two new text object motions:

]]   forward to next function declaration[[   backward to previous function declaration
3]]
d]]
  • It traverses the AST and finds all function declarations and creates a list of those function declarations. During the traversing it also constructs the function signature and all necessary pieces.
  • After that it searches for a function whose offset is larger than the current one via sort.Search
  • Once found it returns the function in Vim’s own format. Note that we have a special vim package that marshal’s the result into a Vim compatible format. This package is based on Alan Donovan’s initial work, which I’ve improved by adding field tag support and fixed couple of bugs. The vim format will be obsoleted in the future, as the latest Vim versions have now native support for encoding/decoding JSON.

Jump to function/type declarations

What about jumping to a function whose name you know already in a given file? Suppose the file itself is over 500 lines and you barely know where the function is declared. However you know the function very well. Or suppose you have a package that is spread over multiple files, wouldn’t it be cool if you could jump from one to another just by giving a function name?

:GoDecls [file]:GoDeclsDir [dir]
  • :GoDeclsDir is the same as :GoDecls, the only difference is it parses all Go files under the current directory. It’s a very powerful tool. What it means is, you can instantaneously jump from one file to another file just by typing a function name. I’m using it for several weeks and now I’m thinking how I was living without it. Just like :GoDecls you can also pass a directory name to it, i.e :GoDeclsDir /usr/local/go/src/bytes
:GoDecls and :GoDeclsDir in action

Software Engineer. Gopher and Coffee geek. Author of vim-go. Look for my open source projects at https://github.com/fatih

Software Engineer. Gopher and Coffee geek. Author of vim-go. Look for my open source projects at https://github.com/fatih