简介
coc.nvim 是 2018 年新开发的 Vim / NeoVim 新一代全代码补全插件,使用 TypeScript 编写,运行于 nodejs 环境。
其代码补全具备快速,可靠,完整 LSP(Language Server Protocol) 功能支持,灵活配置等功能,其追求是将 Vim 打造成与 VSCode 体验一致的现代 IDE 编辑器。
安装
coc.nvim 其本身并不具备代码补全功能,要想完成代码补全,还需安装其他相应语言的 LSP 插件。因此 coc.nvim 其实是一个能加载其他插件的 Vim 插件。
安装步骤如下:
- 首先安装 coc.nvim:
" Use release branch (Recommend)
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" Or latest tag
Plug 'neoclide/coc.nvim', {'tag': '*', 'branch': 'release'}
" Or build from source code by use yarn: https://yarnpkg.com
Plug 'neoclide/coc.nvim', {'do': 'yarn install --frozen-lockfile'}
- 安装所需的语言的补全插件,比如这里我们安装 coc-html,可以用于 HTML 的代码补全:
:CocInstall coc-html
然后打开一个.html
文件,输入内容,即可看到补全(没有补全则按 <Tab> 进行补全),如下图所示:
注:还可以通过在.vimrc
定义全局变量g:coc_global_extensions
,可以在 coc.nvim 服务启动的时候,自动安装多个扩展插件:
let g:coc_global_extensions = ['coc-json','coc-css']
常用的 coc.nvim 插件列表,可查看:coc-extensions
注:可以使用以下命令查看已安装的插件:
:CocList extensions
其中:
?
:表示无效插件
*
:表示插件已激活
+
:表示插件加载成功
-
:表示插件已禁止
使用配置文件
与 VSCode 一样,coc.nvim 也拥有两种配置文件:
-
用户配置:全局配置,配置文件名为:
coc-settings.json
,路径为:$XDG_CONFIG_HOME/nvim
或$HOME/.config/nvim
或$HOME/.vim
。可以通过命令:CocConfig
打开用户配置文件。 -
项目配置:局部配置,配置文件名为:
coc-settings.json
,路径为:.vim
。可以通过命令:CocLocalConfig
打开用户配置文件。
默认配置,用户配置 和 项目配置 会合并并生成最终配置结果,且后面的配置会覆盖前面文件的配置。
注:可以安装扩展插件 coc-json,支持对 json 文件的智能提示,方便编写 coc.nvim 配置文件。
其他
- 检查 NeoVim 状态:
:checkhealth
这里主要关注 coc.nvim 服务状态,如下图所示:
-
查看 coc.nvim 服务相关信息:
:CocInfo
-
卸载插件:
:CocUninstall coc-css
最后附上本人的配置:
-
.vimrc
中的配置:
let g:coc_global_extensions = ['coc-tsserver','coc-html','coc-css', 'coc-json',
\ 'coc-java','coc-python','coc-flutter',
\ 'coc-emmet','coc-snippets','coc-xml','coc-yaml',
\ 'coc-markdownlint','coc-highlight']
" 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.
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" 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>"
" 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>fm <Plug>(coc-format-selected)
nmap <leader>fm <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 <C-d> for select selections ranges, needs server support, like: coc-tsserver, coc-python
nmap <silent> <C-d> <Plug>(coc-range-select)
xmap <silent> <C-d> <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>
- 用户配置:
{
// Enable preselect feature on neovim, default: `true`
"suggest.enablePreselect": true,
// Add preview option to `completeopt`, default: `false`
"suggest.enablePreview": true,
// completion automatically select the first completed
"suggest.noselect": false,
// "suggest.triggerAfterInsertEnter": true
"suggest.minTriggerInputLength": 2,
// Target to show hover information, default is floating window when possible.
"coc.preferences.hoverTarget": "preview",
// Auto close preview window on cursor move., default: `true`
"coc.preferences.previewAutoClose": false
}
注:更多配置选项,请查看:Wiki 或 doc/coc.txt
网友评论