Saturday, March 19, 2016

Integrating cpplint with Vim

Vim is a light-weight, yet powerful editor. Although it may be quite frustrating for developers that it does not, by default, support features such as nice syntax highlighting or keyboard shortcuts,  It is really too easy and straightforward to enable such developer friendly features into it. Here I am showing how to setup a keyboard shortcut to use cpplint with Vim in order to perform code style checking.


cpplint is a code style checker that checks how much your code matches the Google C++ Style guidlines. The feedback returned by cpplint can be valuable for developers to practice writing good code according to Google C++ style guidelines (or others).


Steps:
  1. Download cpplint from here.
  2. copy cpplint to one of your path directories, make sure it is executable. For example:
sudo cp cpplint.py /usr/local/bin/cpplint.py
sudo chmod usr/local/bin/cpplint.py

3. To setup a keyboard shortcut in VIM to run cpplint.py against your cpp file. Add the following line into your ~/.vimrc file

autocmd BufRead *.cpp map <F7> :w<enter>:!/usr/local/bin/cpplint.py %<enter>

4. Now, you can use the F7 keyboard shortcut to run cpplint while editing any *.cpp file.