EvsChen's Blog

Configuring NeoVim for C++ Development

July 05, 2019

Recently I’ve been reading the book Physically Based Rendering. This book gives a detailed document on how to build a physically-based rendering system. And all the chapters and source code is freely available online. Later I will post some notes of this book after I finish reading.

Also it is a good time for me to build up my C++ skills. I didn’t use C++ a lot since my freshmen year. However, it is almost the only language for developing high performance graphics rendering engines. And the first step is, of course, to create a nice development environment for C++. And I choose NeoVim as the editor.

In this post, I will use:

  1. ALE for linting
  2. YouCompleteMe or YCM for autocompletion
  3. ctags for code navigation

ALE

Although ALE stands for Asynchronous Lint Engine, it actually provides a lot more than linting. It is a Language Server protocol that supports lint, go to definition, autocompletion etc. However, the completion is hard to use. So I only use the linting function from ALE and disable the completion by

augroup filetype_cpp
        ...
        autocmd FileType cpp let g:ale_completion_enabled = 0
        ...
augroup END

to improve performance.

ALE has supported a wide range of linters for C++, which can be viewed in :ALEInfo while the buffer is a C++ file. I chose clang. In order for the linter to work properly, the simplest way is to provide a compilation database in JSON format, usually named compile_commands.json. If you’re using CMake, the database can be easily generated by providing a CMAKE_EXPORT_COMPILE_COMMANDS flag. By default, ALE will search for the file under ['build', 'bin'], which can be modified by the option g:ale_c_build_dir_names.

YouCompleteMe (YCM)

To install YCM, better follow the instructions in their repo. I installed the plugin using vim-plug. After the installation, we have to run the install script manually, which is /path/to/your/plugins/YouCompleteMe/install.py. For C language family, it seems that we have to provide a extra conf to YCM, otherwise it will throw a Value Error. To do this, just add

let g:ycm_global_ycm_extra_conf='~/.vim/plugged/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py'

to your .vimrc or init.vim and replace the path with your plugin installation path.

ctags

Although ALE has provided the go to definition function, it usually jumps with a lag and sometimes cannot find the correct definition. So I use ctags. You can install ctags easily using Homebrew or any package manager you like. However, macOS has its own version of ctags so you would want to alias ctags to the version installed by Homebrew before generating the tags file.


EvsChen

Written by EvsChen.
A craftsman and a programmer.