Radu's vim setup

Quick and dirty tips for how to get started with vim and go.

1. Install pathogen

mkdir -p ~/.vim/autoload ~/.vim/bundle
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

And add to the top of ~/.vimrc:

execute pathogen#infect()
syntax on
filetype plugin indent on

2. Install vim-go

git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go

Add these to ~/.vimrc:

let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_build_constraints = 1
autocmd FileType go set number fo+=croq tw=100
autocmd Filetype go set makeprg=go\ build\ .

Make sure $GOPATH is set correctly, start vim and run


You can now use and map things like :GoDef, :GoReferrers, :GoRename etc. My mappings in ~/.vimrc:

autocmd Filetype go nmap <C-]> :exec("GoDef")<CR>
autocmd Filetype go nmap <F9> :GoReferrers<CR>
autocmd Filetype go nmap <C-\> <Plug>(go-def-split)
autocmd Filetype go nmap <Space> <Plug>(go-info)

WARNING: Avoid using foldmethod syntax with vim-go: it can make vim very slow when you open a paren or start a string.

Optional: set up tagbar

Install exuberant-ctags on your system (yes, this is needed even if we are actually going to use gotags). On Debian:

sudo apt-get install exuberant-ctags

Install gotags:

go get -u github.com/jstemmer/gotags
cd github.com/jstemmer/gotags
go build
sudo cp gotags /usr/bin

Install the vim plugin:

cd ~/.vim/bundle
git clone git://github.com/majutsushi/tagbar

Add this to ~/.vimrc:

let g:tagbar_type_go = {
    \ 'ctagstype' : 'go',
    \ 'kinds'     : [
        \ 'p:package',
        \ 'i:imports:1',
        \ 'c:constants',
        \ 'v:variables',
        \ 't:types',
        \ 'n:interfaces',
        \ 'w:fields',
        \ 'e:embedded',
        \ 'm:methods',
        \ 'r:constructor',
        \ 'f:functions'
    \ ],
    \ 'sro' : '.',
    \ 'kind2scope' : {
        \ 't' : 'ctype',
        \ 'n' : 'ntype'
    \ },
    \ 'scope2kind' : {
        \ 'ctype' : 't',
        \ 'ntype' : 'n'
    \ },
    \ 'ctagsbin'  : 'gotags',
    \ 'ctagsargs' : '-sort -silent'
\ }

You can now use or map :TagbarToggle in vim.

Optional: set up ag

Install ag (a faster, better grep) on your system. On Debian:

sudo apt-get install silversearcher-ag

Then install the vim plugin

cd ~/.vim/bundle && git clone https://github.com/rking/ag.vim ag

You can use commands like :Ag in vim now. Here are some useful mappings that you can put in ~/.vimrc:

nmap <F7> :let @/ = "<C-R><C-W>"<CR>:Ag -s -w <C-R><C-W> *<CR>
nmap <F8> :let @/ = "<C-R><C-W>"<CR>:!ag -s -w <C-R><C-W> *<CR>

These search for the identifier under the cursor, and set the search register so you can keep searching within files with n.

CockroachDB logs highlighting

I created a custom syntax highlighting file for log messages. To install, download crlog.vim and put it in ~/.vim/syntax/. It can be used by setting filetype=crlog. I use /tmp/log usually, so I have this in my vimrc:

autocmd BufNewFile,BufRead /tmp/log set filetype=crlog

A screenshot:

Irfan's crlfmt plugin

Irfan Sharif built a plugin to automatically run crlfmt, our style linter for Go code that enforces the CockroachDB Style Guide found here.

git clone https://github.com/irfansharif/vim-crlfmt.git ~/.vim/bundle/vim-crlfmt

Radu's config

You can see my ~/.vimrc here.

My ~/.vim directory is here.