A complete guide to getting started with Co. nvim

introduce

Coc. Nvim is a NodeJS based Vim intelligent completion plug-in for Vim8 and Neovim. Has complete LSP support. Configuration, usage and the overall style of the plug-in system is similar to VSCode.

For myself, the reason for choosing it is that it is easy to install and has enough functionality. The installation is so comfortable compared to YouCompleteme.

See the Wiki for more information.

Also: since CoC itself is just a plug-in of Vim, but it has its own plug-in system, I will call the plug-ins of CoC sub-plug-ins below to distinguish them.

The installation

The premise

Because coc.nvim is based on NodeJS, you need to install NodeJS first if you don’t have NodeJS on your machine.

Vundle

Add the following to.vimrc:

Plugin 'neoclide/coc.nvim'

Run the following commands in Vim:

:source %
:PluginInstall

Vundle versions below 0.10.2 replace plugins with bundles.

NeoBundle

Add the following to.vimrc:

NeoBundle 'neoclide/coc.nvim'

Run the following commands in Vim:

:source %
:NeoBundleInstall

VimPlug

Add the following to.vimrc:

Plug 'neoclide/coc.nvim'

Run the following commands in Vim:

:source %
:PlugInstall

Pathogen

Switch to the torch directory and pull the corresponding repository:

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

Add the plug-in

Since CoC itself does not provide the completion function of specific language, it only provides a platform for completion function, so we need to install specific language service to support the corresponding completion function after the installation is completed. Open Vim and use the following command to automatically install the child plug-ins and their dependencies.

:CocInstall coc-json coc-tsserver

The coc – json coc – tsserver these are corresponding support json, Typescript child plug-ins. Nvim can be found directly on Npm, or managed directly in Vim using the coc-marketplace. The installation command is as follows:

:CocInstall coc-marketplace

After installation, use the following command to open the panel, Tab can be highlighted to install and uninstall the child plug-ins and other operations.

Coclist Marketplace Python is a Python related subplug-in

Up and down can be selected, press Tab to corresponding operation.

There is also a relatively complete list of supported child plug-ins, but I’m not sure if they are.

Modify the configuration

Each built-in function or additional child plug-in can be configured in Vim. Just like the configuration system of VS code. What can be set in the repository Wiki

I don’t use much of it myself, except to add a few related coc-prettier ones:

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

Add the following to ~/.vimrc, which can be selectively completed using Tab and Shift+Tab.

" 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 keyboard shortcuts

To add shortcuts to some common functions, write ~/.vimrc:

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

The preceding configuration adds a shortcut to Prettier :Prettier and the shortcut key Ctrl+F.