简介
coc.vim 是 2018 年新开发的 Vim / NeoVim 新一代全代码补全插件,使用 TypeScript 编写,运行于 nodejs 环境。
其代码补全具备快速,可靠,完整 LSP(Language Server Protocol) 功能支持,灵活配置等功能,其追求是将 Vim 打造成与 VSCode 体验一致的现代 IDE 编辑器。
安装
coc.vim 其本身并不具备代码补全功能,要想完成代码补全,还需安装其他相应语言的 LSP 插件。因此 coc.vim 其实是一个能加载其他插件的 Vim 插件。
安装步骤如下:
- 首先安装 coc.vim:
" 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
文件,输入内容,即可看到补全(没有补全则按
注:还可以通过在.vimrc
定义全局变量g:coc_global_extensions
,可以在 coc.vim 服务启动的时候,自动安装多个扩展插件:
let g:coc_global_extensions = ['coc-json','coc-css']
常用的 coc.vim 插件列表,可查看:coc-extensions
注:可以使用以下命令查看已安装的插件:
:CocList extensions
其中:
?
:表示无效插件
*
:表示插件已激活
+
:表示插件加载成功
-
:表示插件已禁止
使用配置文件
与 VSCode 一样,coc.vim 也拥有两种配置文件:
用户配置:全局配置,配置文件名为:
coc-settings.json
,路径为:$XDG_CONFIG_HOME/nvim
或$HOME/.config/nvim
或$HOME/.vim
。可以通过命令:CocConfig
打开用户配置文件。项目配置:局部配置,配置文件名为:
coc-settings.json
,路径为:.vim
。可以通过命令:CocLocalConfig
打开用户配置文件。
默认配置,用户配置 和 项目配置 会合并并生成最终配置结果,且后面的配置会覆盖前面文件的配置。
注:可以安装扩展插件 coc-json,支持对 json 文件的只能提示,方便编写 coc.vim 配置文件。
其他
- 检查 NeoVim 状态:
:checkhealth
这里主要关注 coc.vim 服务状态,如下图所示:
查看 coc.vim 服务相关信息:
: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 ' to make sure tab is not mapped by other plugin.
inoremap
\ pumvisible() ? "\" :
\ check_back_space() ? "\" :
\ coc#refresh()
inoremap pumvisible() ? "\" : "\"
" Use to trigger completion.
inoremap coc#refresh()
" Use to confirm completion, `u` means break undo chain at current position.
" Coc only does snippet and additional edit on confirm.
inoremap pumvisible() ? "\" : "\u\"
" Or use `complete_info` if your vim support it, like:
" inoremap complete_info()["selected"] != "-1" ? "\" : "\u\"
" Use `[g` and `]g` to navigate diagnostics
nmap [g (coc-diagnostic-prev)
nmap ]g (coc-diagnostic-next)
" Remap keys for gotos
nmap gd (coc-definition)
nmap gy (coc-type-definition)
nmap gi (coc-implementation)
nmap gr (coc-references)
" Use K to show documentation in preview window
nnoremap K :call show_documentation()
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('')
else
call CocAction('doHover')
endif
endfunction
" Highlight symbol under cursor on CursorHold
autocmd CursorHold * silent call CocActionAsync('highlight')
" Remap for rename current word
nmap rn (coc-rename)
" Remap for format selected region
xmap fm (coc-format-selected)
nmap fm (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: `aap` for current paragraph
xmap a (coc-codeaction-selected)
nmap a (coc-codeaction-selected)
" Remap for do codeAction of current line
nmap ac (coc-codeaction)
" Fix autofix problem of current line
nmap qf (coc-fix-current)
" Create mappings for function text object, requires document symbols feature of languageserver.
xmap if (coc-funcobj-i)
xmap af (coc-funcobj-a)
omap if (coc-funcobj-i)
omap af (coc-funcobj-a)
" Use for select selections ranges, needs server support, like: coc-tsserver, coc-python
nmap (coc-range-select)
xmap (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', )
" 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 a :CocList diagnostics
" Manage extensions
nnoremap e :CocList extensions
" Show commands
nnoremap c :CocList commands
" Find symbol of current document
nnoremap o :CocList outline
" Search workspace symbols
nnoremap s :CocList -I symbols
" Do default action for next item.
nnoremap j :CocNext
" Do default action for previous item.
nnoremap k :CocPrev
" Resume latest coc list
nnoremap p :CocListResume
- 用户配置:
{
// 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
参考
- coc.nvim 插件体系 - 介绍