Vim in practice part I

9Jan 2012

Vim in practice part I

This is the first post in a series where I would like to cover the text editor Vim. I’ve been using Vim since many years almost exclusively when editing text, mostly C++ code. Although this article will focus on the aspects I find most useful while programming, it should be a good general introduction.

In this first part, I’ll cover installation and configuration of Vim and some preferred plugins. Later articles will explain what makes Vim so powerful, editing files with it.

Quickstart for the impatient

If you don’t feel like reading this article, here is what it covers in 4 easy steps.

  1. Install Vim by using a packet manager or by downloading it.
  2. Install Vundle with git clone https://github.com/gmarik/vundle.git $HOME/.vim/bundle/vundle
  3. Download the vimrc file and save it to $HOME/.vimrc on Linux or $HOME/_vimrc on Windows.
  4. Run Vim, type :BundleInstall and restart.

Why Vim and why this article

Vim logo

For almost everybody, the first contact with Vim conveys mixed feelings. The editor does not even allow to enter any text by default. Still, there must be something that so many people like it. It takes a while to get used to Vim editing, but once mastered, everything else feels limited. All the keystrokes become so natural that most Vim users press the wrong keys in other applications.

In my opinion, Vim is not a good editor to use occasionally, but the best one for people sitting in front of an editor all day.

Vim is an improved version of the classic vi editor distributed with most Unix systems. It has many improvements over its ancestor, is extremely configurable, and extensible through plugins.

Installation

Most Linux distributions ship Vim as part of their installation. For Debian based systems, the following command installs Vim

$ apt-get install vim     // For the console version
$ apt-get install vim-gtk // For the GUI version

The second command installs Vim with a GUI (GTK+) along the console based version. I tend to use the GUI version, but this is mostly personal preference. I recommend using Vim version 7 or above, since it has many improvements over older versions.

Installation packages for other operating systems can be found on the Vim download page.

Configuration

The Vim experience can be improved by adjusting some of its configuration. On Linux, the configuration file should be placed in $HOME/.vimrc, on Windows in $HOME/_vimrc. See vimrc help.

New users should not try to reconfigure too much of Vim. It is usually better to start with a small configuration and add settings when required. Also, the default settings of Vim are already good for most purposes.

I recommend starting with the following configuration.

set nocompatible          "be iMproved
set enc=utf8              "character encoding is UTF-8
set laststatus=2          "always show status line
syntax on                 "syntax highlighting
filetype plugin indent on "automatic file type handling

You can download the complete vimrc file. It has some additional settings that improve on searching and highlighting, completion menu handling, and has some useful key mappings. The file is documented and should be self explanatory.

Note that the keymappings use the <Leader> key. This is a configurable key, but is set to a backslash () by default. See <Leader> help.

Plugins

As with the configuration, I highly recommend starting with a minimal set of Plugins and add them when required. However, I consider some of them essential for programming and would therefore advice you to install them. Here are the recommended plugins, one by one with a short explanation. We come back to some of them in later articles.

In the following sections, I sometimes refer to commands that can be typed while in Vim. Since we have not covered the different modes that Vim supports (see vim-modes help, it should be noted that these commands are always typed in Normal mode. That is the mode which is entered after the editor starts.

Vundle

Vundle package manager

Vundle is a new and very easy to use plugin manager. I really recommend using it. All plugins presented below will be managed by Vundle. Our vimrc file already includes the required settings for Vundle as well as entries for all Bundles that we are going to install.

In order to use it, we need to install it first. This is the only plugin we need to install manually. It can be done with the following command.

git clone https://github.com/gmarik/vundle.git $HOME/.vim/bundle/vundle

After Vundle has been set up, managing plugins becomes easy. Use :BundleInstall to install bundles, and :BundleInstall! (note the !) to update them. That’s it. Bundles can be removed by deleting the corresponding line in vimrc and calling :BundleClean. It also has options to search for bundles.

For convenience, Git access is provided for all Vim plugins by the Vim Scripts project at its Github Page. This makes it easy to find and use plugins with Vundle, since Github is its default mechanism.

CtrlP

CtrlP is a fuzzy finder for files, buffers, tags, mru. It is completely written in Vimscript and very fast. It makes finding files within a project very comfortable. In normal mode, one types Ctrl-P and a small buffer opens. Files can then be search in different modes, including regex search. By default, just starting to type a file name or part of it should bring it into view. Select the file and hit enter to open it (note that Ctrl-k and Ctrl-j_ works for moving up and down in the list).

C++ editing in Vim

NERD-tree

NERD-tree is a filesystem explorer. Even with CtrlP, I find this plugin useful, mostly for having an explorer-like view of the files, and the ability to operate on them (such as move, delete, etc.).

The main commands are :NERDTreeToggle to open / close the sidebar, and NERDTreeFind to find the place in the filesystem with the file opened in the current buffer. Our vimrc has keymappings for these two commands to make the more accessible.

Tagbar

tagbar shows the ctags generated tags for the current buffer in a sidebar. It orders the tags by scope, which is useful for C++ files.

The main command is :TagbarToggle, to open / close the sidebar. Our vimrc has a keymapping to make this command more accessible.

Ultisnips

UltiSnips stores text snippets that save a lot of typing. These snippets can be configured with shell, Vim, or Python code. A snippet can be always defined, or only for certain file types.

It is useful for things like C/C++ file templates or code snippets that are used frequently. It comes with some predefined snippets.

Wombat

Well, Wombat is not so much of a plugin as the rest, but its a really nice dark colour scheme. If you don’t have any preference, try it. Our vimrc has already set it as default.

Getting Help

The documentation of Vim is very well written and complete. To get help about a topic, enter :help <topic>. For example, to get help about the integrated spelling feature, type the following while in Vim.

:help spelling // Get help about integrated spelling

Alternatively, the Vim documentation is also maintained online at sourceforge

Another good source of information for beginners is the Vim tutorial. It can be started from the shell (on Linux) or from the menu (Windows). It takes an optional two-letter language code.

$ vimtutor    // In the shell
$ vimtutor de // Vim tutorial in German

Providing Help

If you can spare some small amount of money and find Vim or some of the plugins useful, please consider donating. It helps the projects to keep improving.

Tags: vim