How to Set Up Code-Completion for Vim in macOS

Until now, vim is arguably one of the most popular text editor loved for its simplicity and, yes, the awesome shortcuts (command-line mode).

Most developers already satisfied with vim for what it is, but other developers, like me, sometimes miss the code-completion or IntelliSense featured in modern GUI-based text editor.

COC or Conquer of Completion is an intellisense engine for vim8 & neovim by neoclide. It allows vim to have code-completion feature like modern editors.

This post will guide us how to do it in macOS.

Install Vim 8.1

COC needs vim ≥ 8.1 as requirement, if your macOS’s vim version is outdated, it is recommended not to overwrite it. Instead, install another vim with latest version and make an alias. Here’s how to do it:

  1. Install MacVim via Homebrew.
$ brew install macvim
  1. Verify the installation. Make sure it installs the latest version. Normally it will have conflicts with the macOS vim since macvim will installs the same vi binaries, but that’s okay.
$ brew info macvim
==> macvim: stable 8.1-155, HEAD
GUI for vim, made for macOS
https://github.com/macvim-dev/macvim
Conflicts with:
  vim (because vim and macvim both install vi* binaries)
/usr/local/Cellar/macvim/8.1-155 (2,205 files, 35.3MB) *
  Built from source on 2019-03-31 at 21:29:24
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/macvim.rb
==> Dependencies
Required: cscope ✔, lua ✔, python ✔
==> Requirements
Build: xcode ✔
==> Options
--HEAD
 Install HEAD version
==> Analytics
install: 8,594 (30 days), 33,678 (90 days), 210,243 (365 days)
install_on_request: 7,866 (30 days), 30,453 (90 days), 181,256 (365 days)
build_error: 0 (30 days)
  1. Add alias to the Homebrew’s newly installed macvim. Add these lines in ./zshrc or ./bashrc.
alias vim='/usr/local/Cellar/macvim/8.1-155/bin/vim'
  1. Dont forget to call source command to .zshrc or .bashrc.
source ~/.zshrc
  1. Verify it by calling vim command. It should have the latest version now.
    1_5uLFkkue9HZmmSSRoj0mrA

Install vim-plug

vim-plug is a package manager for vim. Download vim-plug and move plug.vim to ~/.vim/autoload directory.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Install COC

You can follow the guide in the official github page for the latest instruction, but, to save your time, i’ll give it below. Although, it will not be guaranteed for future releases.

  1. Make sure you have nodeJS ≥ 8.0, or else download it via the command below.
curl -sL install-node.now.sh | sh
  1. Install yarn.
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
  1. Create directory for COC’s installation.
mkdir ~/.vim/pack/coc/start
  1. Clone COC’s repo.
cd ~/.vim/pack/coc/start
git clone https://github.com/neoclide/coc.nvim.git --depth=1
  1. Install COC.
cd ~/.vim/pack/coc/start/coc.nvim
yarn install
  1. Add Plug for COC. Open ~/.vimrc in editor and add the lines below.
call plug#begin()
Plug 'neoclide/coc.nvim', {'do': { -> coc#util#install()}}
call plug#end()
  1. Open vim and execute :PlugInstall in the command-line mode.
    1_swZr_9KMoxFupTC8mz6teA
  2. COC will be installed via vim-plug. when the installation succeed, you will see the screen below inside vim.
    1_gLK0XFifOzZKFCm89ZrHZw

Install COC Extension

The last steps is the installation of the language server you want to use with the code-completion.

You can see the list of available extensions here. Make sure to follow the requirements of the extensions you want to install as they’re different with each other.

For this example, I will use Java language server.

  1. Execute :CocInstall coc-java in the vim’s command-line mode.
    1_npXesv5yXyyGU6bat8ytdQ
    1_mUokYpSB9-0YUBFI3I61Pg
  2. Open Java source code with vim and see if the code-completion works. Because coc-java requires jdt, and it’s not available yet on my machine, COC will install it the first time I open a Java source code.
    1_yDBzuyGuXrDUsEKmoTYpaw
  3. Re-open the Java source code and try to type to see if the code completion works.

It works like a charm :)

To manage the extensions you can read the guide in the official page.


And that’s how to enable vim’s code-completion in macOS.

Have a nice day and happy coding!

Show Comments

Get the latest posts delivered right to your inbox.