Introduction to Vim

Advanced file editing

Now that you know your way around Vim, you can start using the more advanced functions.

Moving around

There are several ways of moving around in Vim. The first one is using the arrow keys, as you might have already discovered. Vim allows you to be more efficient. To reach the arrow keys you have to move your hands on the keyboard. Therefore it is possible to move the cursor around with the letters h,j,k and l. The disadvantage here is that it only works in Normal mode, but then you are typing text in Insert mode, and should not move around.

<HOME> and <END> allow you to quickly move between the start and end of a line. Sometimes you need some more fine grained control over you movements on a line. Using the movement keys works, but is not the best way. Vim splits your lines into groups of characters, so called words. A word is a group of letters, digits and underscore, separated by whitespace or any other remaining characters, like for instance .,-/_.

To move one word forward in Normal mode, press w. Use b to move one word backwards. By using the capital W (<SHIFT> + w) respective B (<SHIFT> + b) you can jump words which are only separated by whitespace.

Copy, cut and paste

To copy text y is used. For cutting text from the document is done with d. The selection of what to copy or cut can be done in different ways. The first way is to copy or cut a whole line, press the key twice (yy resp. dd). Another way is to use the Visual mode to select what should be copied or cut. The last way is to use the movement commands to select. yw will copy the next word forward, db cuts a word backwards.

To paste the text you copied or cut, use p. This will paste the text after the cursor. Sometimes you want to paste before the cursor (so that the pasted text ends at the current cursor position). To archive this use P (<SHIFT> + p). If you copied or cut a whole line, the line will be pasted below the current line with p or above with P.

Quantify actions

Each command on this page can be prefixed with a number to make Vim to the action n times. So 4h will move 4 positions to the left and 10p will paste whats copied or cut 10 times. You can even quantify combined actions: y4w will copy the next 4 words.

Other useful commands

There is another way to enter the Insert mode from Normal mode. o will insert an empty line below the cursor and switch to Insert mode. If you want to do the same, but start above the current line, use O (<SHIFT> + o).

Action which is good to know is ^w (<CTRL> + w). This is exactly the same as db, but it will work in Insert mode. This way you do not have to change to the Normal mode to remove the last word you wrote. Additionally this works in most command prompts, so you are not only limited to Vim.

Everybody knows the moment when they realize that you just did something you better should not have done. Vim can help you out, as you can undo the last actions by pressing u. To undo the undoing, a simple stroke on U (<SHIFT> + u) is enough.

When programming you quite often want to jump to a specific line number. In Normal mode, a simple Ngg will take you to the start of line N.