Skip to content

Instantly share code, notes, and snippets.

@krlvi
Created August 26, 2018 17:55
Show Gist options
  • Save krlvi/d22bdcb66566261ea8e8da36f796fa0a to your computer and use it in GitHub Desktop.
Save krlvi/d22bdcb66566261ea8e8da36f796fa0a to your computer and use it in GitHub Desktop.

VIM-GO

Plugin https://github.com/fatih/vim-go
Tutorial https://github.com/fatih/vim-go-tutorial
Vimrc https://github.com/fatih/dotfiles/blob/master/vimrc

Run

Commands

  • File :GoRun %
  • Package :GoRun
  • Debug :GoDebugStart

vimrc

Shortkeys

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

Build

Commands

  • Package :GoBuild

vimrc

Auto save on make with

set autowrite

Error navigation

map <C-n> :cnext<CR>  
map <C-m> :cprevious<CR>  
nnoremap <leader>a :cclose<CR>  

Test

Commands

  • File :GoTest from test or implementation
  • Function :GoTestFunc
  • Compile test :GoTestCompile

vimrc

Shortkey to build go implementation or test

" 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>

Coverage

Commands

  • :GoCoverage
  • :GoCoverageClear
  • :GoCoverageToggle
  • :GoCoverageBrowser

vimrc

Toggle coverage

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

Edit

Commands

  • :GoFmt
  • :GoImports
  • Struct add tags :GoAddTags
  • Struct remove tags :GoRemoveTags
  • Manually import package :GoImport <package> - tab completion
  • Manually remove package :GoDrop <package>
  • Import with alias :GoImportAs <alias> <package>

Text objects

  • Inner function - dif, yif, vif etc
  • Whole function - daf, yaf, vaf etc

vimrc

Format on save
let g:go_fmt_autosave = 1

Disable gofmt parse errors
let g:go_fmt_fail_silently = 1

Goimports on save
let g:go_fmt_command = "goimports"

Struct line split
Plug 'AndrewRadev/splitjoin.vim'

  • split gS
  • join gJ

Snippets (Keyword + TAB)
Plug 'SirVer/ultisnips' reference

  • fn fmt.Println()
  • ff fmt.Printf()
  • ln log.Println()
  • lf log.Printf()

Navigation

Commands

  • Between test and implementation :GoAlternate
  • Go to definition :GoDef or gd or ctrl-]
  • Back from definition :GoDefPop or ctrl-t
  • Show navvigation stack :GoDefStack
  • Clear navigation stack :GoDefStackClear
  • File type and function declarations :GoDecls
  • Directory tyle and function declarations :GoDeclsDir
  • jump to next function ]]
  • jump to previous function [[
  • 3]], d]], v]]

vimrc

Needed for :GoDecls and :GoDeclsDir Plug 'ctrlpvim/ctrlp.vim'

Alternate commands

autocmd Filetype go command! -bang A call go#alternate#Switch(<bang>0, 'edit')
autocmd Filetype go command! -bang AV call go#alternate#Switch(<bang>0, 'vsplit')
autocmd Filetype go command! -bang AS call go#alternate#Switch(<bang>0, 'split')
autocmd Filetype go command! -bang AT call go#alternate#Switch(<bang>0, 'tabe')

Documentation

Commands

  • :GoDoc or K
  • :GoDocBrowser
  • :GoInfo function signature
  • Highlight identifiers :GoSameIds
  • Clear highlight identifiers :GoSameIdsClear
  • List files in a package :GoFiles
  • List file dependencies :GoDeps
  • Find references to identifier :GoReferrers
  • Info for identifier :GoDescribe
  • Interfaces a type implements :GoImplements
  • Possible error types :GoWhicherrs
  • Channel info :GoChannelPeers
  • Show possible call targets of cunction call :GoCallees
  • Show function call locations ::GoCallers
  • :GoCallstack
  • Guru scope whole GOPATH :GoGuruScope ...

vimrc

Get info
autocmd FileType go nmap <Leader>i <Plug>(go-info)

Automatically show function info
let g:go_auto_type_info = 1

Function info update delay (ms)
set updatetime=100

Automatically highlight identifiers
let g:go_auto_sameids = 1

Refactor

Commands

  • Rename identifier :GoRename <newname>
  • Show free variables visual select + :GoFreevars

Codegen

Commands

  • Implement interface for type :GoImpl <interface>

Checks

Commands

  • :GoLint
  • :GoVet
  • :GoMetaLinter

vimrc

Meta linter checks for :GoMetaLinter let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck', 'deadcode']

Meta linter on save let g:go_metalinter_autosave = 1

Meta linter checks for autosave let g:go_metalinter_autosave_enabled = ['vet', 'golint']

Syntax highlighting

vimrc

  • let g:go_highlight_types = 1
  • let g:go_highlight_fields = 1
  • let g:go_highlight_functions = 1
  • let g:go_highlight_function_calls = 1
  • let g:go_highlight_operators = 1
  • let g:go_highlight_extra_types = 1
  • let g:go_highlight_build_constraints = 1
  • tab size autocmd BufNewFile,BufRead *.go setlocal noexpandtab tabstop=4 shiftwidth=4

Shortkey suggestion

" Leader keys

" Q

"q Show how function is reached :GoCallstack
autocmd FileType go nmap <leader>q <Plug>(go-callstack)

" W

"w Alternwate window splits
nnoremap <leader>w <C-w>w
"W Only one active window nnoremap <Leader>W :only<CR>

" E

"e Generate error handling :GoIfErr
autocmd FileType go nmap <leader>e <Plug>(go-iferr)

" R

"r Rename :GoRename[!] [to]
autocmd FileType go nmap <leader>r <Plug>(go-rename)

" T

"t Test function :GoTestFunc[!] [expand]
autocmd FileType go nmap <leader>t <Plug>(go-test-func)
"T Test file :GoTest[!] [expand] autocmd FileType go nmap <leader>T <Plug>(go-test)

" Y

"y Keyify struct literals :GoKeyify
nnoremap <Leader>y :GoKeyify<CR>

" U

"u Fill struct with defaults :GoFillStruct
nnoremap <Leader>u :GoFillStruct<CR>

" I

"i List interfaces implemented :GoImplements
autocmd FileType go nmap <leader>i <Plug>(go-implements)
"I Stub interface methods :GoImpl [receiver] [interface]
nnoremap <Leader>I :GoImpl<CR>

" O

" P

"p c Possible callers of a function :GoCallers
autocmd FileType go nmap <leader>pc <Plug>(go-callers)
"p t Possible call targets for a type :GoCallees autocmd FileType go nmap <leader>pt <Plug>(go-callees)
"p v Possible vars of a pointer :GoPointsTo autocmd FileType go nmap <leader>pv <Plug>(go-pointsto)
"p e Possible error types of an error :GoWhicherrs
nnoremap <Leader>pe :GoWhicherrs<CR>
"p c Channel peers :GoChannelPeers autocmd FileType go nmap <leader>pp <Plug>(go-channelpeers)

" A

"a Alternate test/implementation :GoAlternate[!]
autocmd FileType go nmap <leader>a <Plug>(go-alternate-edit)

" S

"s Describe type or identifier :GoDescribe
autocmd FileType go nmap <leader>s <Plug>(go-describe)

" D

"d Show doc :GoDoc [word]
autocmd FileType go nmap <leader>d <Plug>(go-doc)
"D Browse doc :GoDocBrowser [word]
autocmd FileType go nmap <leader>D <Plug>(go-doc-browser)

" F

"f Show all functions and types in file :GoDecls [file]
nnoremap <Leader>f :GoDecls<CR>
"F Show all functions and types in directory :GoDeclsDir [dir]
nnoremap <Leader>F :GoDeclsDir<CR>

" G

"g Show definition jump stack :GoDefStack [number]
autocmd FileType go nmap <leader>g <Plug>(go-def-stack)
"G Clear definition jump stack :GoDefStackClear
autocmd FileType go nmap <leader>G <Plug>(go-def-stack-clear)

" H

"h Show all identifiers referring to the object :GoReferrers
autocmd FileType go nmap <leader>h <Plug>(go-referrers)
"H Toggle highlight same identifiers :GoSameIdsToggle
nnoremap <Leader>H :GoSameIdsToggle<CR>

" J

" K

" L

"l List buffers
nnoremap <leader>l :ls<CR>:b<space>
"L List files
nnoremap <Leader>L :Explore<CR>

" Z

" X

"w Close window split
nnoremap <leader>x <C-w>c

" C

"c Toggle coverage :GoCoverageToggle[!] [options]
autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)
"C Browse coverage :GoCoverageBrowser[!] [options]
nnoremap <Leader>C :GoCoverageBrowser<CR>

" V

"v Show free variables in selection :GoFreevars
autocmd FileType go nmap <leader>v <Plug>(go-freevars)

" B

"b Build :GoBuild[!] [expand] :GoTestCompile[!] [expand]
autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>
"B Run :GoRun[!] [expand]
autocmd FileType go nmap <leader>B <Plug>(go-run)

" N

" M

"m Meta linter :GoMetaLinter [path]
autocmd FileType go nmap <leader>M <Plug>(go-metalinter)

" Extra

"gd Go to definition :GoDef
"<C-t> Back from definition :GoDefPop [count]
nnoremap <leader><TAB> :b#<CR>
nnoremap <CR> :nohlsearch<CR><CR>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment