Monday, March 14, 2022

vim as an IDE for C++

I found a relatively quick way to setup vim / neovim as an IDE for C++. Here is how it works.

  1. vim loads a plugin called Coc (I pronounce it coque which is a family-friendly approach).
  2. Coc runs a nodejs process to perform runtime checks.
  3. This process starts a configured language server (such as ccls or clangd) and then communicates with it.

 Here is what we need to set it up.

  1. Install ccls.
  2. Configure ccls by using a .ccls file at the root of the project, or by generating a compile_commands.json file as with the set(CMAKE_EXPORT_COMPILE_COMMANDS on) option.
  3. Install a relatively new version of nodejs (I installed 16.14.0 LTS).
    1. curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
    2. sudo bash nodesource_setup.sh
    3. sudo apt install nodejs
  4. Install the Plug plugin management system in vim.
    1. curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  5. Edit .vimrc to add the neoclide/coc plugin, the long list of key bindings from coc for convenience, and then reopen vim, running the command :PlugInstall.
  6. Configure coc using :CocConfig and editing the JSON that shows up. Refer here.
  7. Open a C++ source file in your project in vim / neovim.

No comments: