Mac-Neovim-Coc configuration and common shortcut keys

Mac-Neovim-Coc configuration and common shortcut keys

Recommended reference:

Vim Cheat Sheet

Vim Tutorial In Playground

1. Installation

Install Nodejs and Prerequisite

# Installation Prerequisite brew install node sudo npm i -g neovim yarn #After installation, check node -v Copy code

2. plug-in installation

  • In order to facilitate the use of the management Coc plug-in on all computers, use the following to install the plug-in,
the let G: coc_global_extensions = [ 'COC-JSON' , 'COC-vimlsp' ] duplicated code
  • View all currently installed plugins:
    :coclist extension
    • Browse: Ctrl+j/k navigate all extensions
    • Uninstall, disable plug-ins, etc.: Tab
    • Exit: Esc
    • The asterisk indicates that the current file is being activated
      • The sign indicates that it has been installed, but the current file is not in use.
  • View all the plug-ins that can be installed:
    :cocInstall coc-marketPlace
    • View all the plug-ins that can be installed:
      :coclist marketplace
    • Browse: Ctrl+j/k navigate all extensions
    • After selecting a plug-in, enter can be installed.

3. init.vim Configuration configuration

Basic Setup

set encoding=utf- 8 set hidden set shortmess+= c set updatetime= 100 Copy code

The automatic completion of Tab, Shift+Tab switching takes effect

"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 ()? "\<Cn>" : /<SID> check_back_space()? "\<TAB>" : /coc#refresh() inoremap <expr> <S-TAB> pumvisible ()? "\<Cp>" : "\<Ch>" function ! S : check_back_space () ABORT the let COL = COL ( '.' -) . 1 return ! COL || getline ( '.' ) [ COL - . 1 ] = # ~ '/S' endfunction The duplicated code

Use carriage return to confirm the completion instead of line feed.

It is possible to type only two characters and press enter to complete the function.

"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 () /: "\<Cg>u\<CR>\<cr>=coc#on_enter()\<CR>" Copy code

The next one reports an error, the previous one reports an error

"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 <the Plug> (COC-diagnostic- Next ) copying the code

View function definition, call

"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 <the Plug> (COC-References) copying the code

Show document

"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' ) the else Execute '!' . & keywordprg. "" . the expand ( '<cword>' ) endif endfunction The duplicated code

Highlighting the same word. You can use vim-illuminate

"The Symbol and the Highlight When ITS Holding References The Cursor. Autocmd the CursorHold * Silent Call CocActionAsync ( 'highlight' ) copying the code


"Symbol renaming, and. Nmap <Leader> RN <the Plug> (COC- the rename ) Copy the code

Operate the selected code

"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) Copy code

quickfix code action

"Remap Keys for Applying to codeAction at The Current Buffer. Nmap <Leader> AC <the Plug> (COC-codeaction) " the Apply to problem AutoFix ON at The Current Line. Nmap <Leader> QF <the Plug> (COC- FIX -CURRENT) Copy Code

Quickly select the entire function or class

"Objects text class and the Map function " NOTE: . The Requires 'textDocument.documentSymbol' Language Support Server from The xMAP IF <the Plug> (COC-funcobj-I) OMAP IF <the Plug> (COC-funcobj-I) xMAP AF <the Plug > (funcobj- COC- A ) OMAP AF <the Plug> (funcobj- COC- A ) xMAP IC <the Plug> (COC-classObj-I) OMAP IC <the Plug> (COC-classObj-I) xMAP AC <the Plug>(classobj- COC- A ) OMAP AC<The Plug> (classobj- COC- A ) copying the code

4. coc-settings.json basic configuration

It is recommended to get the completion directly through the coc plug-in instead of configuring the language server by yourself.

Open coc-settings:


For basic configuration, please refer to the LSP prompt to see what it means.

"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" } } } } Copy code

4.1 C, CPP LSP configuration


brew update brew install ccls Copy code

In Vim, run: CocConfig to open the configuration file of coc.nvim and add the following lines to it. Add these to 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" } } } } } Copy code

In order to prevent the header file from reporting errors, you also need to add something to find the header file to 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:

Remember to put it under init..Optins, refer to Coc-ccl's github wiki for details

-isystem /usr/local/include -isystem /Applications/ -isystem /Applications/ .1/the include -isystem /Applications/ -isystem /Applications/ .14 .sdk/usr/the include duplicated code

Specific different projects need to generate an index

Yes, refer to the front and back parts of the Linux kernel as an example.

Need to install Bear, or use CMake to generate


4.2 Java LSP

Just install coc-java, after opening nvim and java files, will be downloaded automatically

4.3 Go

Install Golang on MacOS

brew update brew install go Copy code

After the installation is over, source zshrc or restart the shell to use

go env
See if the environment can be displayed.

Subsequent vim-go TODO update