How do I set up LSP-powered autocompletion in Vim with coc.nvim?
Answer
:CocInstall coc-tsserver
Explanation
coc.nvim (Conquer of Completion) is a powerful plugin that brings Language Server Protocol (LSP) support to Vim and Neovim. It provides IDE-level features like intelligent autocompletion, go-to-definition, hover documentation, and diagnostics.
How it works
coc.nvim runs as a Node.js extension host that communicates with language servers. After installing the base plugin, you add language-specific extensions using :CocInstall. Each extension connects to the appropriate language server and provides context-aware completions as you type.
The completion popup appears automatically with fuzzy matching. You navigate candidates with <C-n> and <C-p>, and confirm with <CR>. The plugin also shows function signatures, documentation snippets, and diagnostic errors inline.
coc.nvim uses a coc-settings.json file (opened with :CocConfig) for configuration, following the same JSON format as VS Code settings. This makes it familiar to users coming from VS Code.
Example
Install coc.nvim and configure key mappings in your .vimrc:
Plug 'neoclide/coc.nvim', {'branch': 'release'}
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"
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)
nmap <silent> K :call ShowDocumentation()<CR>
Then install language extensions:
:CocInstall coc-tsserver " TypeScript/JavaScript
:CocInstall coc-pyright " Python
:CocInstall coc-json " JSON
:CocInstall coc-rust-analyzer " Rust
With gd mapped, pressing it on any symbol jumps to its definition across files. gr shows all references, and K displays hover documentation in a floating window.