Setting up Vim for C++ development

Vim is a powerful text editor that supports multiple programming languages and comes with a whole host of features such as a multi-level undo tree, powerful search and replace, and an extensive plugin system allowing for customisation. One advantage of using a text editor over an IDE is speed. The official Vim C++ Wiki page contains some useful tips for using Vim with C++.

Managing Vim plugins

Plugins are useful for adding extra functionality to Vim. The Vundle (Vim bundle) plug-in manager for Vim installs, updates, cleans, searches and configures plugins. Installing Vundle  is the first step for setting up Vim for C++ development.

Installing a Vim plugin from GitHub using Vundle requires three simple steps:

  1. Clone the plugin repository to your ~/.vim/bundle/ directory
  2. Update your ~/.vimrc file with the new plugin details
  3. Install the plugin by launching Vim and running :PluginInstall

To check what plugins are installed, startup Vim and type :PluginInstall and a list of installed plugins will appear.

Code completion for C++

The YouCompleteMe code completion engine for Vim supports several programming languages and specifically uses a Clang-based engine for C++ code completion. It also includes a semantic engine triggered by typing .  or -> or :: as well as diagnostics.

To install the YouCompleteMe Vim plugin:

  1. Clone the YouCompleteMe Git repository to your ~/.vim/bundle/ directory
  2. Add the line Plugin 'Valloric/YouCompleteMe' to your ~/.vimrc file
  3. Install the plugin by launching Vim and running :PluginInstall
  4. Compile YouCompleteMe by running the command ./ --clang-completer from within the ~/.vim/bundle/YouCompleteMe directory.
  5. Afterwards, you need to provide compile flags to libclang in order for the semantic analysis to work for C/C++. Typically, a compilation database is generated by CMake (or your build system).

Syntax checking for C++

The syntastic plugin checks for syntax errors in the code, before compiling. This can be installed using the Vundle plugin manager. This guide for configuring syntastic is good.

To install the syntastic Vim plugin:

  1. Clone the syntastic plugin Git repository to your ~/.vim/bundle/ directory
  2. Add the line Plugin 'vim-syntastic/syntastic' to your ~/.vimrc file
  3. Syntastic relies on external checkers in order to perform syntax checks, so these should be added to your ~/.vimrc file.
  4. Install the plugin by launching Vim and running :PluginInstall
  5. Check that everything is installed correctly by running :SyntasticInfo (the C++ checkers you installed should be listed in the output)

Debugging for C++

When it comes to debugging C++ code, some people choose to debug in Vim whilst others prefer to switch to an IDE. Clewn integrates the GNU debugger (gdb)  into the Vim editor which allows for gdb command completion, breakpoints, watch variables etc.

Tags for C++ code

When browsing large C++ projects it can be useful to view an outline of the classes with their associated methods shown. The tagbar plugin provides this functionality by displaying a tags in a window, ordered by scope.

An almost Vim IDE for C++

The yavide project aims to create an Integrated Development Environment (IDE) similar to Eclipse or Visual Studio in Vim. This is an ambitious task and thus far includes a bunch of plugins working together to create this functionality. Features include a project explorer, project builder, class browser, code auto-completion, code navigation, code syntax highlighting and auto-formatting, as well as code management.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s