推荐参考:
一、Installation
安装Nodejs 和Prerequisite
# 安装Prerequisite
brew install node
sudo npm i -g neovim yarn
# 安装后,检查
node -v
二、插件安装
- 为了方便在所有的电脑上使用管理Coc插件,使用如下来安装插件,
let g:coc_global_extensions = [ 'coc-json', 'coc-vimlsp']
- 查看当前安装的所有插件:
:coclist extension
- 浏览:Ctrl+j/k navigate all extensions
- 卸载,禁用插件等:Tab
- 退出:Esc
- 星号表示正在当前文件启用
-
- 号表示已经安装,只不过当前文件没有使用中。
- 查看所有可以安装的插件:
:cocInstall coc-marketPlace
- 查看所有可以安装的插件:
:coclist marketplace
- 浏览:Ctrl+j/k navigate all extensions
- 选择一个插件后,enter就可以安装。
- 查看所有可以安装的插件:
三、init.vim Configuration配置
Basic Setup
set encoding=utf-8
set hidden
set shortmess+=c
set updatetime=100
Tab,Shift+Tab切换自动补全生效
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
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
使用回车确认补全,而不是换行。
可以做到只打两个字符,按回车补全功能。
" Make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
下一个报错,上一个报错
" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
查看函数定义,调用
" GoTo code navigation.
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 Leader+h to show documentation in preview window.
nnoremap <silent> <LEADER>h :call <SID>show_documentation()<CR>
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
elseif (coc#rpc#ready())
call CocActionAsync('doHover')
else
execute '!' . &keywordprg . " " . expand('<cword>')
endif
endfunction
Highlighting相同单词。可以使用vim - illuminate
" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')
重命名
" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)
对选中代码进行操作
" Applying codeAction to the selected region.
" Example: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)
quickfix code action
" Remap keys for applying codeAction to the current buffer.
nmap <leader>ac <Plug>(coc-codeaction)
" Apply AutoFix to problem on the current line.
nmap <leader>qf <Plug>(coc-fix-current)
快速选中整个函数或者类
" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)
四、coc-settings.json基本配置
推荐通过coc插件直接获得补全,而不是自己配置language server。
打开coc-settings::cocconfig
基本配置,具体可以参考LSP的提示看看是什么意思。
"coc.preferences.extensionUpdateCheck": "daily",
"coc.preferences.snippetStatusText": "‚ìà ",
"coc.preferences.messageLevel": "error",
"diagnostic.errorSign": "\uf467",
"diagnostic.warningSign": "\uf071",
"diagnostic.infoSign": "\uf129",
"diagnostic.hintSign": "\uf864",
"suggest.completionItemKindLabels": {
"class": "\uf0e8",
"color": "\ue22b",
"constant": "\uf8fe",
"default": "\uf29c",
"enum": "\uf435",
"enumMember": "\uf02b",
"event": "\ufacd",
"field": "\uf93d",
"file": "\uf723",
"folder": "\uf115",
"function": "\u0192",
"interface": "\uf417",
"keyword": "\uf1de",
"method": "\uf6a6",
"module": "\uf40d",
"operator": "\uf915",
"property": "\ue624",
"reference": "\ufa46",
"snippet": "\ue60b",
"struct": "\ufb44",
"text": "\ue612",
"typeParameter": "\uf728",
"unit": "\uf475",
"value": "\uf89f",
"variable": "\ue71b"
},
"codeLens.enable": true,
"diagnostic.displayByAle": false,
"diagnostic.checkCurrentLine": true,
"diagnostic.virtualTextPrefix": " ❯❯❯ ",
"diagnostic.virtualText": true,
"coc.preferences.formatOnSaveFiletypes": [
"json",
"python",
"c",
"cpp"
],
"snippets.ultisnips.directories": [
"$HOME/.config/nvim/Ultisnips/",
"$HOME/.config/nvim/plugged/vim-snippets/UltiSnips/"
],
"languageserver": {
"ccls": {
"command": "ccls",
"filetypes": [
"c",
"cpp",
"objc",
"objcpp"
],
"rootPatterns": [
".ccls",
"compile_commands.json",
".vim",
".git/",
".hg/"
],
"initializationOptions": {
"cache": {
"directory": "/tmp/ccls"
}
}
}
}
4.1 C, CPP LSP配置
brew update
brew install ccls
In Vim, run :CocConfig to open the configuration file of coc.nvim and add the following lines to it.添加这些内容到coc-settings.json
{
"languageserver": {
"ccls": {
"command": "ccls",
"filetypes": [
"c",
"cpp",
"objc",
"objcpp"
],
"rootPatterns": [
".ccls",
"compile_commands.json",
".vim/",
".git/",
".hg/"
],
"initializationOptions": {
"cache": {
"directory": "/tmp/ccls"
}
}
}
}
}
为了防止头文件报错,还需要添加,寻找头文件的东西到coc-settings.json
If you are using macOS, then chances are ccls cannot find system headers and as a result reports a bunch of errors.
This is because new macOS systems moves system headers into the macOS SDK directory and no longer places them in /usr/include. And the reason why ccls can find the system headers previously is that /usr/include is hard-coded into ccls during compilation. But now, since the macOS SDK path is not hard-coded, it cannot find them any more.
I personally solved the issue by manually adding the path of the system headers to .ccls. Here is how to get the path:
Run g++ -E -x c++ - -v < /dev/null in your terminal and you’ll see a list of include paths that the compiler searches. They are between #include <...> search starts here: and End of search list.. Now put them into your .ccls file as -isystem options (unlike -I, the errors and warnings in the header files found in -isystem paths are ignored by the syntax checker).
After manually adding these system header paths, the .ccls file might look like this: 记得放到init..Optins下面,具体参考Coc-ccl的github wiki
-isystem
/usr/local/include
-isystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
-isystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/include
-isystem
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
-isystem
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include
具体不同的项目需要生成一个索引compile_commands.json
就可以,参考以Linux内核为例的前后部分。
需要安装Bear,或者使用CMake生成compile_commands.json
4.2 Java LSP
安装coc-java即可,在打开nvim和java文件后会自动下载jdt.ls
4.3 Go
brew update
brew install go
安装结束后,source zshrc或则重启shell
使用go env
查看是否可以显示环境。
后续vim-go TODO更新