# ankursinha.in/blog

neuroscience/fedora/musings

Fri 12 June 2015

# Using Vim for C/C++ development efficiently

Posted by ankur in Tech (964 words, approximately a 4 minute read)

Vim is truly a great editor. (Before you begin to yell, so is Emacs.) I've used Vim for quite a while now, and have collected certain plug-ins over time to improve my workflow. This post just collates this information - it's all take from various sources on the internet and I will provide references at the end of the page.

## Touch typing

Perhaps the most critical feature of Vim is that it enables touch typists to work extremely efficiently. If you don't know what touch typing is, now is a great time to learn. See those two protrusions on the F and J letters of your keyboard? They're there for a reason - that way you know where you "home row" is all the time. Vim is designed in a way that ensures that you never have to lift your hands off the home row. I dislike moving my hand to use the mouse, and I'll briefly discuss what I do to reduce mouse usage to a minimum too. A great tool available on most Linux distributions is "gtypist". It has sets of exercises that teach you to touch type. On a Fedora system, you can install it by using DNF:

sudo dnf install gtypist


## Vim modes and switching between them

Vim has three main modes that are used frequently - you should use vimtutor to learn them and how to use them efficiently:

• Insert mode: where it's basically a simple text editor - only use this when you're writing text, not for moving, not for copy pasting, not for anything, only when you enter text.
• Command mode: where you can use Vim commands to do things - move around efficiently, find, replace, copy, paste, run recorded command sequences, save, quit, and so on.
• Last line mode: similar to command mode, but instead of using keyboard short cuts to run commands, you write them on a command prompt

When Vim starts, you're in command mode. To move to insert mode, you simply press "i" and start typing. To move from command mode to last line mode, you press ":". This brings up a ":" on the last line and you can enter commands there, like ":wq" which is used to save(w) and quit(q) the file.

Moving from insert mode back to command mode requires you to press Esc (escape). Now, this annoyed me, because escape isn't near the home row, but there is a historical reason for why Esc is still the chosen key. The good news is that you don't have to use Esc to get out of insert mode, you can use one of the many alternatives - Ctrl +C, or Ctrl+[ and so on and always keep your fingers on your home row.

## Plug-in installation and maintenance

Before I discuss plug-ins, a little bit about maintaining them. They all go into the ~/.vimrc directory, but keeping them sane across different machines is sometimes quite a pain. I've now been using pathogen to install plug-ins - it makes things really easy. To maintain plug-ins, I use git, of course! Most plug-ins are housed on github, and you can simply clone them into the bundle directory that pathogen uses. To make it even easier, you can add them as git sub modules. Now, you can set up a new machine, clone your vim files repository and you're good to go! My vimfiles directory is here. Obviously, it has various customisations, so it'll probably be better if you use it only as a reference.

## Plugin 1 - CVim - an IDE for C/C++

CVim adds all sorts of nifty features to Vim. For instance, you have commands that add template classes - declarations, comments, implemetations; hot keys to quickly compile and run your commands and so on. The hotkey cheatsheet will give you a good idea of what this plugin provides.

## Plugin 2 - cscope/ctags

These aren't vim plug-ins, rather they are utilities that add tagging support to Vim. You can use these to find and jump to function calls, funciton and variable declarations and so on. They come in really handy, especially when you're reading code. On Fedora, you can install them using DNF:

sudo dnf install cscope ctags


There are cscope related plugins that will solve common issues too. For example, the cscope-autoload plugin comes in handy - it automatically loads the cscope database file for you when you open a programme source code file. It's the only one I use at the moment - quite enough.

## Plugin 3 - neocomplete

Vim already has a basic completion feature in it. Neocomplete adds to that - completes all sorts of things. Have a look at the github page, they have screenshots.

## Plugin 4 - syntastic

As the main page says: Syntastic is a syntax checking plugin for Vim that runs files through external syntax checkers and displays any resulting errors to the user. It comes in really handy - even when I'm writing my blog posts in rst.

## Keeping your fingers on the home row outside Vim

• I rarely use the mouse to switch between windows. "Alt + Tab"; "Alt + ~" work nicely enough. This is why I really like Gnome3 - I don't need a bottom panel with windows listed on it.
• For Firefox, I use the Pentadactyl extension - This basically lets you use vim keybindings in Firefox. It's brilliant - you rarely need to use the mouse.
• On the terminal I use byobu-tmux - it let's you use multiple terminal screens that you can cycle between nicely using only key board shortcuts. Use any number of TUI programs you want with this - irssi, ncmpcpp, vim, rtorrent, vit (for taskwarrior) and so on - no need to use the mouse at all.