Niek Sanders' Travels

{ Home }

{ Photos }

{ Schedule }

{ Biography }

{ Education }

{ Litterbox }

{ Coding }

{ Links }

{ Sex }

Vim Tips for Programmers

This page is nothing more than a collection of cool features in the Vim text editor that programmers might find useful. I only cover the stuff (and commands) that I personally use. There are other neat features such as folding which simply aren't part of my usual repetoire.


Split Screen

You can "split" your Vim session in to multiple windows. You can have multiple windows looking at one file, or you can have different files open in different windows. Vim supports both horizontal and vertical splits. This technique works best for people who maximize the size of their terminals. (I.e. don't just use the default tiny xterm size).

I use this all the time, keeping my header file open in one view and my source file open in the other. The advantage to this over using two separate xterm windows is that you can yank stuff in the vim buffer from one split and paste it into the other.

Example 1
vim foo.h
:vsplit foo.cpp

Hold down "control" and hit "w" twice switch which split is active. There are also other navigation commands available, but that's the one I use most often.

Example 2
vim hugebar.h
hsplit

The second example makes a horizontal split on the same file. This can be used to have one split looking at the variable declarations and the other split looking at some functions further up the file.


Visual Select Modes

Open up a text file in Vim. Press down "shift" and hit "v". Use the j/k keys to navigate up and down. See how vim is highlighting a selection of text? You can do all sorts of cool stuff with this.

Hit the "y" key once and you've yanked the selection into the buffer. Navigate elsewhere and hit "p" and you'll have pasted that buffer somewhere else.

It gets better. This selection feature integrates cleanly with all the usual Vim tricks. Want to find and replace in just those selected lines?

Example 1
:'<,'>s/meow/woof/g

Note that the first '<,'> part automatically appeared when I hit the colon key (while having stuff visually selected). The stuff I wrote after that specifies that all instances of meow be replaced with woof.

Another super cool trick usual visual select in line mode. Select a bunch of C/C++ include directives and type !sort -u. Just the lines you highlighted will be run through the UNIX sort program. (The -u flag indicates that duplicates should be discarded). You've just alphabetically sorted your header includes.

Another visual select mode can be activated pressing "ctrl" followed by v. This is the block mode. Try moving around your cursor now. Note that you can actually select blocks of text. You can highlight a section and press "c" for change. The highlighted stuff will dissapear. Anything you write will be copied over to all the highlighted areas. (Once you hit escape).

Again using the block mode.... try making a highlight column one character wide but several lines high. Then hit "I". You are now prependding text to all those columns at once. This feature works especially well will highly structured text (HTML, program source code, etc).


Macros

Look at the following text:

cat,2.0,meow
dog,1.0,woof
pig,3.0,oink

Let's say you want to flip the text of the second and third columns. You could write a one line call to awk to do this for you. Searching for the awk syntax sucks up valuable time. I just abuse vim in to doing the transformation for me.

Example 1
qa
/,
l
ctrl-v
n
h
x
$
p
h
ctrl-v
N
l
x
hh
p
0
j
q
10@a

So that example looks like a bunch of gobbly-gook. However, the only important commands are "qa", "q", and "10@a". The "qa" command says store all the stuff I'm doing into Macro A. The "q" stops recording macro commands. The last "10@a" command says repeat it 10 times. (Note that vim nicely stops for you if you repeat too many times; I always pick a much larger repeat than actually needed). All the stuff in between the first and last command was just me movin the text. I used navigation commands taking advantage of the structure (e.g. commas) so that it doesn't matter how many spaces or characters are in the text I'm navigating. The last step of my navigation is moving to the beginning of the next line. That way, I can just replay the macro as many times as I want.

That was a pretty touch example. When I was writing up that tutorial, I realized I had forgotten to end my example lines with line breaks. I did the following:

Example 2
qa
$
a
<br>
escape
0
j
q
@a
@@ (repeated till all the lines have BR's).

Again macros look more intimidating than they actually are. The second example takes me under two seconds to record. This time, I use the "@@" command which repeats the last macro. I just keep hitting it manually so I get exactly the number of repetitions I want. There are more elegant ways, but sometimes the brute force method is just fine.