VIM Advanced Guide
The basics of the VIM editor were explained in the previous tutorial. This tutorial continues with more advanced key combinations available in VIM.
Knowing more key combinations speeds up your editing skills. Before you continue, make sure Caps-Lock is off; this works ultimately confusing in VIM.
More Movement keys
These are some keys to make cursor movement in VIM smoother. Please make yourself familliar with these key combinations first. You can use all kinds of direction-commands in conjunction with modifier commands below.
Key Action w, W Move to the next word/token. The uppercase W versions moves the cursor to the next space-separated word. e, E Move to the end of a word/token. The uppercase E version moves the cursor to the next space-separated end. b, B Move to the begin of a word/token. The uppercase B version moves the cursor to the previous space-separated begin.
The change command
The c key can be used to change things. In combination with the movement keys you specify what you want to change. You can also include a number to repeat the command many times.
Key Action cw Changes one word/token. cb Changes the beginning of a word/token. 3cw Changes 3 words at once! C The uppercase version changes the remaining part of a line.
Other combinations with movement keys are also possible, but I leave that to your imatination. Those combinations follow the same logic.
The delete command
The same rules used for the change command also apply for the delete command. The delete command is d. In other words, 3dW removes 3 entire words, and the uppercase D removes the remaining of a line.
There is one special case, and that's dd. That command deletes the entire line, but also stores it's contents in the paste-buffer.
Key Action dw Deletes one word/token. Also very useful to remove indenting in source code. db Deletes the beginning of a word/token. 3dw Deletes 3 words at once! D The uppercase version deletes the remaining part of a line. dd Delete/cuts the entire line
Cut, Copy and Paste
Off course, VIM does support copy and paste. If you're an experienced user you might like the support of multiple clipboards. I don't use this functionality yet. Everything you delete, will be moved to the clipboard as well; delete works like cut.
Key Action x Cut/delete one character. 3x Deletes 3 characters. X Just like backspace; cut/delete the character at the other side of the cursor. dd Cut/delete one line. yy Yanks (copies) one line; the same logic used for other commands. yw Yanks (copies) one word. p Paste the clipboard content after the cursor. P Parse the clipboard content before the cursor.
The v command starts visual mode. This allows you to select a piece of text. Afterwards, use y to copy the text, d to delete it, or : to open a prompt (for example to run a search-replace command on the selection).
Key Action v Start visual mode, used to select text. V Same as the lowercase version, but only select entire lines.
Undo and redo
VIM has advanced support for undo and redo, but the repeat command is the most effective, it's almost a macro processor.
u Undo the last operation. U Undo's all operations made in the current line. Ctrl+R Redo the last undo. Unfortunately, r was already taken for 'replace'. . Repeat the last command. This is very useful in combinations with other advanced editing commands like A (append to the end), I (insert at the begin), or c3w (change 3 words). The reason is, you don't have to move the cursor for these kind of commands.
Search and replace
Search and replace can be a little difficult in VIM, although it is very powerfull at the same time. VIM uses regular expressions for searching, which is complex for a beginner, but a powerfull tool for the expert user. If you don't know anything about regular expressions, just remember to put a \ token before every non-alpha character. For example, /usr/bin becomes \/usr\/bin.
Key Action /text Search for text, a prompt appears where you can type the text to search. ?text Same as the / command, only backwards. # Searches backwards for the text under the cursor. n, N Jump to the next or previous match. :%s/text/replace/g Perform a global search and replace. The "%" token means: "all lines". You can replace it with a line number or range (this will automatically happen with v; visual mode). Without a range specifier, only the current line will be searched. If you omit the "g" switch, only the first match will be replaced.
Other useful commands are:
Key Action J Joins two lines. ~ Change the case of a character, and avance to the next character. Ctrl+V+(one character) (within insert mode) In insert mode, this allows you to type special characters. For example, Ctrl+V+M displays a ^M symbol (a Cr newline character), which can be used in the command :%s/^M//g to remove all Cr newlines from a Windows .txt file. Ctrl+A, Ctrl+X Increment, or decrement the first number found after the cursor.
Advanced movement keys
There are some other useful movement commands that I didn't include in the previous section below, because that would be confusing. I do think they are useful to know as well:
Key Action h, j, k, l You can use the keys h, j, k and l to move the cursor as well; in case you're too lazy to move your fingers to the arrow keys, or want to move the cursor faster. 0 The 0 (zero, not 'O') command moves the cursor to the first character. ^, $ The ^ and $ commands move the cursor to the begin and end of the line text respectively. If you ever used regular expressions with grep, sed, awk, Perl or PHP, you might reconize these symbols. gg, G Jump to the begin/end of a file. Note that these key combinations also work in the less command. Ctrl+G Tells where where you are, and what file you're editing. Ctrl+F, Ctrl+B The same as PgUp and PgDn. I often use F=forward, B=backward as mnemonic. :number <enter>, numbergg Jump to a line number in a file.
Advanced insertation commands
Instead of the familiar insert command (i) all the time, you can use other methods to start insert mode. These commands allow you to modify text faster at different locations in a line.
Key Action I Insert text at the beginning of a line (switches to insert mode). a Append the text after the cursor, instead of inserting it before the cursor. This command is really useful in combination with the e command; for example ea allows you to append text at the end of a word, and you don't have to move the cursor at all! A Append text to the end of a line (switches the insert mode). o, O Open a line after, or before the current line. This saves you from another useles "move cursor", "insert", "right", "enter" key combination. r (one character), R Replace one character, for example rt replaces the current characer with a "t". The R command starts replace-mode which allows you to replace more characters at once. numbers Substitute a number of characters (erases them, and switches to insert mode).