vim配置golang環境,主要使用了兩個插件: coc.nvim
和vim-go
參考文章:
- https://zhuanlan.zhihu.com/p/102306124
- https://mounui.com/331.html
vim的簡單效果


vim的基礎配置
set cmdheight=2
set conceallevel=0
set nu
syntax on
syntax enable
set autoindent
set laststatus=2
set ruler " 在狀態欄顯示光標的當前位置,位於哪一行那一列
" set cursorline
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
" 鼠標
set mouse=a
set selection=exclusive
set selectmode=mouse,key
set showmatch
" set paste
set nocompatible
set showmode " 在底部顯示,當前處於的模式
set showcmd " 在底部顯示當前鍵入的指令
set t_Co=256
filetype indent on " 開啟文件類型檢查,並且載入與該類型對應的縮進規則。比如,如果編輯的是.py文件,Vim 就是會找 Python 的縮進規則~/.vim/indent/python.vim
set textwidth=80 " 設置行寬,一行顯示多少字符
set wrap " 自動折行,太長的話分成幾行顯示
" set nowrap " 關閉自動折行
set linebreak " 只用遇到制定的符號(空格、連詞號等標點符號),才發生折行,不會再單詞內部折行
set wrapmargin=2 " 制定折行處與編輯窗口的右邊緣之間空出的字符數
set scrolloff=5 " 垂直滾動時,光標距離頂部/底部的位置(單位:行)
set sidescrolloff=15 " 水平滾動時,光標距離行首或行尾的位置(單位:字符),該配置在不折行時比較有用
set laststatus=2 " 是否顯示狀態欄,2表示顯示 0 不顯示 1 多窗口才顯示
" =============== 搜索 ================
set hlsearch " 高量顯示搜索結果
set incsearch " 每輸入一個字符,就自動跳到第一個配置的結果
set ignorecase " 忽略大小寫
set smartcase " 如果同時打開了ignorecase,那么對於只有一個大寫字母的搜索詞,將大小寫敏感
" ================= 編輯 ==================
" set spell spelllang=en_us " 拼寫檢查 英語
set nobackup " 不創建備份文件 該文件的標志是,原文件名的末尾,加了一個波浪號
set noswapfile " 不創建交換文件,交換文件的作用是系統崩潰時恢復文件,文件的開頭是. 結尾是.swp
set undofile " 保留撤銷歷史
set undodir=~/.vim/.undo//
set autochdir " 自動切換工作目錄
set noerrorbells " 出錯時,不要發出響聲
set visualbell " 出錯時,閃屏
set history=1000 " vim記住多少次歷史操作
set autoread " 打開文件監視,如果在編輯過程中文件發生外部改變,會提示
set autowrite
set wildmenu
set wildmode=longest:list,full " 命令模式下,底部操作指令按下tab自動補全,第一次tab,會顯示所有匹配的操作指令清單,第二次tab,依次選擇各個指令
"==============================================================================
" 主題配色
"==============================================================================
" 開啟24bit的顏色,開啟這個顏色會更漂亮一些
set termguicolors
call plug#begin('~/.config/nvim/plugged')
" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'
" vim airline
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
" vim startify
Plug 'mhinz/vim-startify'
" fzf
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
" indentLine
Plug 'Yggdroot/indentLine'
" vim_go
Plug 'fatih/vim-go'
" Use release branch (Recommend)
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" nerdtree
Plug 'scrooloose/nerdtree'
" auto-pairs
Plug 'jiangmiao/auto-pairs'
" gitgutter
Plug 'airblade/vim-gitgutter'
" auto-save
Plug '907th/vim-auto-save'
" surround
Plug 'tpope/vim-surround'
" vim-easymotion
Plug 'easymotion/vim-easymotion'
" Initialize plugin system
call plug#end()
" ============== 插件配置 ================
" ============== airline =====================
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline_theme='luna'
let g:airline_powerline_fonts = 1
" set guifont=DejaVu Sans Mono for Powerline Book:h13
" ================== indentLine ===============
let g:indentLine_color_term = 239
let g:indentLine_color_tty_light = 7 " (default: 4)
let g:indentLine_color_dark = 1 " (default: 2)
let g:vim_json_syntax_conceal = 0 "" 為了防止導致json的引號自動隱藏
let g:indentLine_noConcealCursor="" " 為了防止導致json的引號自動隱藏
" nerdtree
map <C-n> :NERDTreeToggle<CR>
" map <F8> :NERDTreeToggle<CR>
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
"
" auto-pairs
let g:AutoPairsFlyMode = 1
let g:AutoPairsShortcutBackInsert = '<M-b>'
" auto-save-file
let g:auto_save = 1 " enable AutoSave on Vim startup
" ================ easy-motion ========================
let g:EasyMotion_do_mapping = 0 " Disable default mappings
" Jump to anywhere you want with minimal keystrokes, with just one key binding.
" `s{char}{label}`
nmap s <Plug>(easymotion-overwin-f)
" or
" `s{char}{char}{label}`
" Need one more keystroke, but on average, it may be more comfortable.
nmap s <Plug>(easymotion-overwin-f2)
" Turn on case-insensitive feature
let g:EasyMotion_smartcase = 1
" JK motions: Line motions
map <Leader>j <Plug>(easymotion-j)
map <Leader>k <Plug>(easymotion-k)
" ===================== easy-motion ===========================
" ===================== coc.nvim ==========================
" if hidden is not set, TextEdit might fail.
set hidden
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup
" Better display for messages
set cmdheight=2
" You will have bad experience for diagnostic messages when it's default 4000.
set updatetime=300
" don't give |ins-completion-menu| messages.
set shortmess+=c
" always show signcolumns
set signcolumn=yes
" Use tab for trigger completion with characters ahead and navigate.
" Use command ':verbose imap <tab>' to make sure tab is not mapped by other plugin.
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" Use <c-space> to trigger completion.
inoremap <silent><expr> <c-space> coc#refresh()
" Use <cr> to confirm completion, `<C-g>u` means break undo chain at current position.
" Coc only does snippet and additional edit on confirm.
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
" Or use `complete_info` if your vim support it, like:
" inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "\<C-y>" : "\<C-g>u\<CR>"
" Use `[g` and `]g` to navigate diagnostics
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
" Remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
" Use K to show documentation in preview window
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
" Highlight symbol under cursor on CursorHold
autocmd CursorHold * silent call CocActionAsync('highlight')
" Remap for rename current word
nmap <leader>rn <Plug>(coc-rename)
" Remap for format selected region
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)
augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s).
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
" Update signature help on jump placeholder
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end
" Remap for do codeAction of selected region, ex: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)
" Remap for do codeAction of current line
nmap <leader>ac <Plug>(coc-codeaction)
" Fix autofix problem of current line
nmap <leader>qf <Plug>(coc-fix-current)
" Create mappings for function text object, requires document symbols feature of languageserver.
xmap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap if <Plug>(coc-funcobj-i)
omap af <Plug>(coc-funcobj-a)
" Use <TAB> for select selections ranges, needs server support, like: coc-tsserver, coc-python
nmap <silent> <TAB> <Plug>(coc-range-select)
xmap <silent> <TAB> <Plug>(coc-range-select)
" Use `:Format` to format current buffer
command! -nargs=0 Format :call CocAction('format')
" Use `:Fold` to fold current buffer
command! -nargs=? Fold :call CocAction('fold', <f-args>)
" use `:OR` for organize import of current buffer
command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
" Add status line support, for integration with other plugin, checkout `:h coc-status`
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" Using CocList
" Show all diagnostics
nnoremap <silent> <space>a :<C-u>CocList diagnostics<cr>
" Manage extensions
nnoremap <silent> <space>e :<C-u>CocList extensions<cr>
" Show commands
nnoremap <silent> <space>c :<C-u>CocList commands<cr>
" Find symbol of current document
nnoremap <silent> <space>o :<C-u>CocList outline<cr>
" Search workspace symbols
nnoremap <silent> <space>s :<C-u>CocList -I symbols<cr>
" Do default action for next item.
nnoremap <silent> <space>j :<C-u>CocNext<CR>
" Do default action for previous item.
nnoremap <silent> <space>k :<C-u>CocPrev<CR>
" Resume latest coc list
nnoremap <silent> <space>p :<C-u>CocListResume<CR>