Complete Guide to getting started with coc.nvim

Time:2021-8-7

Complete Guide to getting started with coc.nvim

introduce

Coc.nvim is a VIM intelligent completion plug-in for vim8 and neovim based on nodejs.
Complete LSP support. The overall style of configuration, usage and plug-in system is similar to vscode

For myself, the reason for choosing it is that it is easy to install and has enough functions. Compared with youcompleteme, the installation process is simply too comfortable.

For more information, see wiki

Another: because COC itself is only a plug-in of vim, but it has its own plug-in system, I will call the plug-in of COC as a sub plug-in to distinguish it.

Complete Guide to getting started with coc.nvim

install

premise

Because coc.nvim is based on nodejs, if there is no nodejs on the machine, you need to install nodejs first

Vundle

Add the following to.vimrc:

Plugin 'neoclide/coc.nvim'

stayVimRun the following command inside:

:source %
:PluginInstall

Vundle version less than0.10.2WillPluginreplace withBundle.

NeoBundle

Add the following to.vimrc:

NeoBundle 'neoclide/coc.nvim'

stayVimRun the following command inside:

:source %
:NeoBundleInstall

VimPlug

Add the following to.vimrc:

Plug 'neoclide/coc.nvim'

stayVimRun the following command inside:

:source %
:PlugInstall

Pathogen

Switch to the pathogen directory and pull the corresponding warehouse:

cd ~/.vim/bundle
git clone https://github.com/neoclide/coc.nvim

Add plug-in

Because COC itself does not provide the completion function of specific languages, it only provides a platform for the completion function,
Therefore, after installation, we need to install specific language services to support the corresponding completion functions.
Open vim and use the following command to automatically install sub plug-ins and related dependencies.

:CocInstall coc-json coc-tsserver

amongcoc-json coc-tsserverThese are the corresponding sub plug-ins that support JSON and typescript.
To retrieve which sub plug-ins are available, you can directly find coc.nvim on NPM,
Or use COC marketplace to manage directly in vim. The installation commands are as follows:

:CocInstall coc-marketplace

After installation, use the following command to open the panel,TabYou can install and uninstall the highlighted sub plug-ins.

#Open panel
:CocList marketplace

#Search Python related child plug-ins
:CocList marketplace python

Complete Guide to getting started with coc.nvim

Use up and down to select, and press tab to perform corresponding operations.

In addition, there is a relatively complete list of supported sub plug-ins here, but it is uncertain whether it is comprehensive.

Modify configuration

Various built-in functions or additional sub plug-ins can be configured in vim. Just like the configuration system of vscade. See the wiki of the warehouse for specific settings

I don’t use much myself, I just addcoc-prettierRelated:

{
  "prettier.singleQuote": true,
  "prettier.trailingComma": "all",
  "prettier.bracketSpacing": false
}

stay~/.vimrcYou can use tab and shift + tab to select and complete the following contents.

" Use <Tab> and <S-Tab> to navigate the completion list
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

Add common shortcuts

Add shortcut keys for some common functions. You can~/.vimrcWrite in:

" Use <Ctrl-F> to format documents with prettier
command! -nargs=0 Prettier :CocCommand prettier.formatFile
noremap <C-F> :Prettier<CR>

A shortcut command of prettier is added to the above configuration:PrettierAnd added the shortcut key Ctrl + F