Vim (or MacVim) as a C/C++ IDE

Mon, Sep 1, 2014 4-minute read

This post is meant to be a little guide to setting up Vim (or MacVim) as your next favorite C/C++ IDE. Vim is probably the most powerful text editor that humans have ever created, so you should consider using it even if its learning curve is a bit steep.

MacVim

First, let’s install MacVim. MacVim adds a graphical interface to Vim that can help newcomers (only for OSX users, others should consider GVim). Skip the next steps if you want to use the regular Vim instead.

  • Get the latest snapshot of MacVim from here (make sure you have the correct OSX version).
  • Decompress it (double click) and drag MacVim.app to your Applications folder.
  • Open the Terminal (commonly located in /Applications/Utilities/Terminal.app, but I suggest you put it in your Dock) and go to the MacVim Snapshot folder you just downloaded. In my case, I had to type the following:
    cd ~/Downloads/MacVim-snapshot-73/
  • Copy the mvim binary to your PATH so that you can open MacVim from any directory in your computer. Like this (you will have to enter your machine password):
    sudo cp mvim /usr/bin
  • Now you should be able to start a new document with MacVim by typing:
    mvim hello.c
    (Type :w to quit).

.vimrc

Now that you have Vim (and/or MacVim) installed in your system, the next thing you should do is to set it up to adapt it to your needs. The rest of the instructions work for Vim and MacVim, so I will use Vim to refer to both (simply replace vim for mvim if you want to use MacVim). Vim is highly configurable, but here I will describe what I believe is the most useful setup for new users and C/C++ developers.

The Vim (or MacVim) configuration file is located in ~/.vimrc. Therefore, type vim ~/.vimrc to start editing your configuration. You might see a blank file if you have never edited Vim’s configuration before. You can copy and paste the following. There’s a comment for the most important commands, so that you can understand what these lines do if you are interested.

Plugins

Now it’s time to install some plugins. We will start with Pathogen, a script to make the installation of other scripts extremely easy. To install Pathogen, just type:

mkdir -p ~/.vim/autoload ~/.vim/bundle<br /> curl https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim > ~/.vim/autoload/pathogen.vim<br />

Using Pathogen, we will install Syntastic, a wonderful plugin to identify errors in your code (linting) in the most popular programming languages. To install it, just type:

cd ~/.vim/bundle &#038;&#038; \<br /> git clone https://github.com/scrooloose/syntastic.git

Now restart Vim and type :Helptags. Linting should be working now for C/C++.

The next plugin to install is MiniBufExplorer, which will make our life easier when dealing with multiple files in Vim. To install it, simply type:

cd ~/.vim/bundle &#038;&#038; \<br /> git clone https://github.com/fholgado/minibufexpl.vim.git

We also want to have some auto complete options when developing our C/C++ programs. The best plugin I’ve found is called YouCompleteMe, which is compatible with basically all the most popular programming languages to date. Type the following to install it:

cd ~/.vim/bundle &#038;&#038; \<br /> git clone https://github.com/Valloric/YouCompleteMe.git<br /> cd YouCompleteMe<br /> git submodule update --init --recursive<br /> ./install.sh --clang-completer

Note: You might need to install Homebrew to install YouCompleteMe. To install it, type:

<br /> ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"<br />

You will need to do some additional setup in order to get the plugin working. As you may see in the second to last line of the .vimrc above, we set up the default configuration file in ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py. You can simply copy my .ymc_extra_conf.py in this folder. To do so, simply type:

curl /drop/.ycm_extra_conf.py > ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py

Finally, the last plugin to install is the C Plugin, which will help us when navigating through C/C++ files, debugging, etc. This plugin does not support Pathogen installation, so we will have to do it manually. Go to the official plugin page: http://www.vim.org/scripts/script.php?script_id=213 and download the latest cvim.zip. Copy the zip file to your ~/.vim/ folder and type:

unzip cvim.zip

Before we are done, you should configure the template file with your name and affiliation. To do so, edit the template file and edit the user macros, like this:

vim ~/.vim/c-support/templates/Templates

My User Macros look like this:

` § ========================================================== § User Macros § ==========================================================

If you’ve done everything correctly, you will be able to create a new .c file (e.g. vim hello.c and a nice template will be included in the top of the file. Moreover, you should be able to open multiple files (e.g. :e otherfile.c) and they will appear in the MiniBufferExplorer. And finally, if you make some errors while coding, Syntastic will tell you. As an example, see the following screenshot:

Please, let me know if you have any questions. I hope you enjoy your new IDE!