Skip to content

Instantly share code, notes, and snippets.

@mcastelino
Last active July 23, 2019 17:36
Show Gist options
  • Save mcastelino/156e552a7e8554fe9db00b646065f91f to your computer and use it in GitHub Desktop.
Save mcastelino/156e552a7e8554fe9db00b646065f91f to your computer and use it in GitHub Desktop.
vim setup for go

Based on https://unknwon.io/setup-vim-for-go-development/ && https://github.com/fatih/vim-go-tutorial

Pathogen and pathogen plugins

Setup Pathogen

https://github.com/tpope/vim-pathogen

Install plugins

cd ~/.vim/bundle
git clone https://github.com/tpope/vim-sensible.git
git clone https://github.com/fatih/vim-go.git
git clone https://github.com/Shougo/neocomplete.vim.git
git clone https://github.com/majutsushi/tagbar.git
git clone https://github.com/scrooloose/nerdtree.git
git clone https://github.com/sjl/gundo.vim
ls ~/.vim/bundle/
gundo.vim  neocomplete.vim  nerdtree  tagbar  vim-dispatch  vim-go  vim-sensible

Color Scheme

mkdir -p ~/.vim/colors/
cd ~/.vim/colors/
curl -O https://raw.githubusercontent.com/fatih/molokai/master/colors/molokai.vim

Other tools

go get github.com/jstemmer/gotags
go get github.com/fatih/motion

Auto Completion

necomplete this needs gocode to work properly

go get github.com/nsf/gocode

The rest get installed via vim-go

:GoInstallBinaries
~/go/bin$ ls
asmfmt  errcheck  git-validation  gocode  godef  gogetdoc  goimports  golint  go-md2man  gometalinter  gomodifytags  gorename  gotags  guru  impl  keyify  motion

.vimrc

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

:let mapleader = ","

setlocal cinoptions=:0
setlocal spell spelllang=en_us
set nu

"To stop the annoying highlighting of words
"Enable this if you are
set nospell

"Hit F5 before pasting to prevent the auto indent"
set pastetoggle=<F5>

"Gundo
if has('python3')
    let g:gundo_prefer_python3 = 1          " anything else breaks on Ubuntu 16.04+
endif
nnoremap <F6> :GundoToggle<CR>

"Tagbar to see current file tags
nmap <F8> :TagbarToggle<CR>


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'
\ }


"vim-go
let g:go_disable_autoinstall = 0

" Highlight
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1

" neocomplete
let g:neocomplete#enable_at_startup = 1

" color theme
colorscheme molokai
let g:molokai_original = 1
let g:rehash256 = 1

" Nerdtree
map <C-n> :NERDTreeToggle<CR>

" From Fathi's tutorial
" Save on build
set autowrite
let g:go_list_type = "quickfix"
map <C-j> :cnext<CR>
map <C-k> :cprevious<CR>
nnoremap <leader>a :cclose<CR>

autocmd FileType go nmap <leader>r  <Plug>(go-run)

" run :GoBuild or :GoTestCompile based on the go file
function! s:build_go_files()
  let l:file = expand('%')
  if l:file =~# '^\f\+_test\.go$'
    call go#test#Test(0, 1)
  elseif l:file =~# '^\f\+\.go$'
    call go#cmd#Build(0)
  endif
endfunction

autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>

autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)

let g:go_fmt_command = "goimports"

let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck']
let g:go_metalinter_autosave = 1
let g:go_metalinter_deadline = "5s"

autocmd FileType go nmap <Leader>i <Plug>(go-info)
let g:go_auto_type_info = 1
set updatetime=100

let g:go_auto_sameids = 1

vim build configuration

Disabling python3 as it seems to have issues with gundo (and I do not want to debug)

./configure --with-features=huge  \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-pythoninterp=yes \
            --with-python-config-dir=/usr/lib/python2.7 \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
            --prefix=/usr \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment