shell

Vim for programmers (I)

Vim is suitable for a myriad of applications. However, it is in programming where it specially shines. Vim offers facilities that make the programming workflow especially easy and efficient. Some people use newer editors (or IDEs) just because they think Vim lacks some new cool features. That is not the case as I will show you in this issue.

(I will use C source files in the examples, but most of the information in this post is applicable regardless of the language programming you use)

Syntax highlighting
Vim supports syntax highlighting for hundreds of file types. Having a look at /usr/share/vim/vim64/syntax (this is for my linux system, it may vary on your system) reveals 420 different syntax files (one file per file type). Among these, it supports all of the most popular languages (c, c++, basic, yacc, lex, java, html, xml, eiffel, perl…). In addition, Vim also recognizes some other file formats as alsaconf, xf86conf, etc. If Vim has been properly set up after installation, it should detect the file type and color the content accordingly. If that is not the case, you can enable/disable syntax highlighting with :syntax on/off. The picture below depicts three different files (C, bashrc and xorg.conf) with syntax highlighting activated:

vim_syntax_example

Line numbers
Sometimes it is convenient to see line numbers at the left of the window. It helps to quickly locate the line you are interested in. You can do this with :set number or :set nu. To dissable it, use :set nonu[mber]

vim_lines_numbers

Parenthesis matching
Vim matches parenthesis and square brackets as you type if the showmatch option is enabled (:set showmatch). If you want to check if your parenthesis are properly balanced, you can place the cursor at one of the parenthesis and type % in command mode. This will move the cursor to the matching parenthesis (if it exists). This works with square and curly brackets too.

Vim counts for you
This is specially cool in my opinion. Ctrl-a and Ctrl-x increments/decrements the number the cursor is placed on. This is especially good because Vim understands different bases. So, typing Ctrl-a in the following scenario:

0x09
0xa

The same works for octal numbers (those that have a leading zero) and, of course, decimal numbers. While this can seem just incidental, it turns out to be really useful if combined with repetition. What is the hexadecimal representation of 1564 (decimal)? Just do the following:

0x0

And now 1564 Ctrl-a ESC (this tells Vim to do Ctrl-a 1564 times). This is what you get:

0x61c

Indentation
Indentation is obviously an important aspect of writing software: it makes the code readable. Vim helps you in this task adding support for indentation. We need to set some global options. You can use these three:

  • :set autoindent
  • :set smartindent
  • :set cindent

The first one sets up a basic indentation. The second one provides an improved one, suitable for C and other programming languages. The third one establishes the specific indentation for C files. I use these three but you should try them and keep only those you feel comfortable with.

Another command to use for indentation is = (equal). It “filters” the text with an external program (if specified) or with the internal C indentation routine. To specify an external filter, you have to set the equalprg variable:

:set equalprog=indent

Note that the definition for filter is “program that accepts text at stantard input, changes it in some way, and sends it to the standard output”. Not all programs match this criteria. Regardless of the rutine used to filter the text, = indents the line where the cursor is placed or the selected text if any.

Tip: G=gg indents the whole file. This is very useful when copy-pasting from another window, remote session, etc, messes the indentation up. In the picture below, the left window shows a code copied from the Internet and the right one, the same text after doing G=gg

vim_indentation

You can always indent text blocks freely if the automatic indentation rules do not fit your taste. In order to do this, use the <> commands. Applied twice on a text object they indent that text to the left or to the right.

Commenting out blocks of code
Some times, you need to comment out several lines. There is a quick way to do this and it involves using the free selection command (Ctrl-v). With it you can select portions of text freely. The trick is that after selecting the text, if you type I (capital i), write something at the beginning of the line and press ESCESC, what you just wrote will be inserted at the beginning of all the rest of the selected lines. So if we select the beginning of the line, type I, write // and then ESCESC, those characteres (used as comment markers in C++) will be inserted in each of the selected lines.

Conclusion
This is the first part of “Vim for programmers”. It just shows some of the features that can be used for programming (though most of them are just useful at any time). In the next issue I will cover more of the aspects that make Vim a great editor for programmers.

Vim Sheet (V)

  • :syntax on/off Enable/disable syntax highlighting
  • :set nu/nonu Enable/disable line numbers
  • % matches parenthesis, square brackets and curly braces
  • Ctrl-a, Ctrl-x Increment/decrement numbers in different bases.
  • =, <<, >> Indent text.

1 Comment

  • On 08.03.10 commandliners » Vim. Saving keystrokes: abbreviations said:

    […] tool if you want to save time in your daily work. It helps you in the write-compile-debug cycle, it indents and autoindents code, it is extremely powerful for searching, replacing and many other frequent […]

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields