Skip to content

Instantly share code, notes, and snippets.

@MarioCarrion
Last active November 17, 2023 22:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save MarioCarrion/99f6a6110796cff5df118822472a0bc9 to your computer and use it in GitHub Desktop.
Save MarioCarrion/99f6a6110796cff5df118822472a0bc9 to your computer and use it in GitHub Desktop.
Nvim Go configuration
"-- vim-go specific configuration
" 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)
autocmd FileType go nmap <leader>t <Plug>(go-test)
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')
autocmd FileType go setlocal foldmethod=expr foldexpr=getline(v:lnum)=~'^\s*'.&commentstring[0]
let g:go_list_type = "quickfix" " error lists are of type quickfix
let g:go_fmt_command = "goimports" " automatically format and rewrite imports
let g:go_auto_sameids = 1 " highlight matching identifiers
"-- vim-go specific configuration (END)
"-- coc.nvim specific configuration
set hidden
set cmdheight=2
set updatetime=300
set shortmess+=c
if has("patch-8.1.1564")
set signcolumn=number
else
set signcolumn=yes
endif
nmap <silent> gr <Plug>(coc-references)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> rn <Plug>(coc-rename)
nnoremap <silent> K :call <SID>show_documentation()<CR>
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
else
call CocAction('doHover')
endif
endfunction
"-- coc.nvim specific configuration (END)
@MarioCarrion
Copy link
Author

@bytegolang For nvim, you have two options, either you add it to your init.vim configuration or you add it to the ftdetect folder.

I like using ftdetect because it keeps the configuration separated per file type, so in this case I'd copy this file to ~/.config/nvim/ftdetect/go.vim this way this configuration is loaded when Go files are detected.

@bytegolang
Copy link

@MarioCarrion thank you!!!

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