How to Set Up Code Completion for VIM/NeoVIM

This will be a quick guide on how to Set Up Code Completion for VIM/NeoVIM. When my friends mentioned about VIM/NeoVIM in our conversation, they all criticized the lack of code-completion or IntelliSense featured in modern GUI-based text editor. Fortunately, there are ton of plugins could make it done and easy. There’s deoplete, ncm2, coc, mucomplete, and probably some others.

Code Completion engine for NeoVim

NeoVim supports some basic functionality out of the box through omnicomplete, but we’re going to replace that with something much more powerful. coc.vim a.k.a Conquer of Completion is an intellisense engine for NeoVIM/VIM. It’s built upon the concept of language servers, which power features like auto-completion, go-to-definition, hover tooltips, and more in modern editors.
Since NeoVIM v 0.4, it was supported floating windows, which allows the programe to display a content floating windows. Floating Window is very important feature to display code completion, function’s document …
💡 Note: Neovim and Vim are almost the same software in practice, that’s why I refer to them in this article under the global name Vim.

Why is coc.vim

Over the years I tried VimCompletesMe , YouCompleteMe, deoplete.nvim, OmniCppComplete-neovim but I could not find anything that was robust, provided good suggestions and supported all programming languages that I a work with on a regular basis. That was until recently when I found coc.vim 🥂 It comes with several major features that are the crux of bringing Vim to the same level as modern IDEs.

  1. It was incredibly easy to set up and immediately worked with just a few lines of configuration.
  2. You’ll get: Intellisense code engine, auto-completion, linting, code fixing, … almost powerful features in one.
  3. It’s built upon language servers, which power intellisense in many modern editors.

How to Set Up Code Completion for VIM/NeoVIM

To get started, install the package via your package manager. vim-plug is a package manager for NeoVIM/VIM. I recommend that you adopt vim-plug as your plugin manager if you don’t use one already.

Plug 'neoclide/coc.nvim', {'branch': 'release'}

If you dont’ use vim-plug, check out these installation examples
To confirm it’s all working by using :checkhealth and :CocInfo

How to setup code completion for VIM
NeoVim Health with COCInfo

You found the coc.vim version in the right panel like the image above. That means coc.nvim is now installed. But, you won’t notice much difference.
To open completion popup when press Tab button at a part of words, let’s inserting mapping code below into ini.vim

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

Save ini.vim then reload $MYVIMRC or re-open your current file. When you start typing you should notice the completion menu:
Completion from words in buffers and file paths completions are supported by default.

Using coc extensions

Did you known? Coc extensions can be forked from VSCode extensions providing a better user experience. So, the big advantage of coc.nvim is it can load extensions forked from VSCode, which have more features most of the time. You can toy around with it. Without extension, you only get partial support from language server. That why coc extensions are needed.
There 3 ways to install a coc extension:

  • Using :CocInstall like
    :CocInstall coc-json coc-css
  • With shell script, use command like:
    # install coc-json & coc-html and exit
    vim -c 'CocInstall -sync coc-json coc-html|q'
  • Use vim’s plugin manager for coc extension:
    Plug 'neoclide/coc-tsserver', {'do': 'yarn install --frozen-lockfile'}
    I don’t prefer this method because you won’t uninstall the extension by using :CocUninstall and that automatic update support is not available.

Use the command :CocConfig to open your user configuration file
Currently, I’m using Intelephense PHP language server, here is my config of language server in coc-settings.json:

{
    "languageserver": {
        "intelephense": {
            "command": "intelephense",
            "args": ["--stdio"],
            "filetypes": ["php"],
            "initializationOptions": {
                "storagePath": "/tmp/intelephense"
            }
        }
    },
}

Use the command :CocConfig to open your user configuration file.

Conclusion

So far, Neovim has been pretty good to me, although the new process model means that it’s pretty hard to write functions that invoke an external process that takes interactive input from a user. If you like me have been jealous of the awesome IntelliSense in VSCode but don’t want to switch to another editor then coc gives you a way to have your 🍰 and 🍴 it too. Try it out, you will not regret it!