As a developer you gotta love Vim! It is easily available on almost any server you login to. Most of the times, it will already be installed. You can customize it to your needs, but you should be comfortable with its basic functionalities as you don’t want to tweak it on every server. I thus like to customize it a little bit, but try to stick close to the default. But the best thing about Vim is that after some time learning it, it is so much faster and easier to edit text than any other editor.

Today, I will show you some tricks in Vim of how to do things more efficiently.

Vim modes

Vim is a modal editor. When you start it up, you are in normal mode which is the mode you are in most often. It might feel odd at first, because in normal mode you don’t write, but rather you navigate around your code / text. As it turns out that is really useful. From normal mode, you can get to insert mode by pressing i or to visual mode by pressing v (there are more options, of course). To get back to normal mode, simply press Esc.

Searching in Vim

When you are in normal mode, you start a search by hitting up / and then type what you are looking for followed by the Return key. The good thing is: after you searched for a word, you can search for the next occurrence simply by pressing n (think n for next). To go to the previous result, press N. If you want to search backwards from the beginning, use ? rather than /. In that case, n refers to the next result going backwards and N going forward, so they reverse. Essentially, n is the next hit in the search direction and N in the opposite direction.

Repeating edits with a dot

The . is such a helpful command in Vim as it allows you to repeat the last edit again. A common pattern in Vim is to have one key for a movement and the dot to repeat a previous edit. For example you search for a word and edit it somehow. Then you press n to find the next occurrence of that word and press . to repeat the edit. One key to move and one key to edit - how awesome!

In my example, I am adding the text Hello with the o command which starts to edit a new line below the cursor. Then, I can simply repeat that by pressing . which now stands for Insert Hello to a new line. Note how you can also prepend the . with a number to repeat the edit x times.

Undo and redo changes in Vim

The u command is easy to remember for undo. Simply press it in normal mode to undo the most recent edit. Note that an edit could be a few characters, some words or even whole sentences. You yourself can control how much the undo will affect by chunking your edits to appropriate sizes. A good indicator is your thought process. When you note that you are thinking and making a pause, switch back to normal mode, so you have a natural point to come back to using undo. Of course, sometimes you press u, but you didn’t want to undo or you pressed it too many times. Don’t worry, simply use Ctrl-r to redo the change again:

Search for words using * and #.

Another nice pair of commands are * and #. These are great shortcuts to search for the current word under the cursor (or the one closest to the cursor). This is incredibly useful when you are programming and you want to find the occurrence of a variable. Simply press * and Vim will find the next occurrence searching forward in the document. Pressing # does the same, but searching backwards.

Important to note: once you used them, you can again also use n and N for the next / previous result. Why is that useful? It’s simple: imagine you search with * for the word odd and you rename it to even. Were you to press * now, then Vim would search for even, however by pressing n Vim will search for the next result of the previous search which was odd, so you can find additional occurences of odd and rename them as well (use the . to do so!)

Auto-complete text

Vim comes with a simple auto-complete solution. It basically looks in the current document for a best guess to auto-complete what you are currently typing. To invoke it, use the command Ctrl-N in insert mode.

Recording macros in Vim

A very powerful aspect of Vim is that you can quickly record custom macros and then replay them. Macros are saved in so-called registers (whose content you can peak into by typing :reg from normal mode).

There is a register for each letter of the alphabet a-z. To start recording a macro, you simply press q followed by the letter of the register you want to save the macro to. In my example I use qw which will record the macro to register w. After starting the recording, any keystroke will be put into that register. To finish the recording, press q again.

Here, I record a macro to put HTML tags around a word which I then replay for other words.

To replay a macro, you use the command @ followed by the register name again (here: @w as we recorded to register w using qw).