Mastering VIM

Last issue we looked at Emacs, the kitchen-sink text editor used by l33t hackers and other advanced users. Emacs’ advantage is that you need never leave its extensive confines if you don’t want to. It combines text editor with debugger, psychiatrist, change viewer, and mail reader. Not everyone wants this: many people want their text editor to be a text editor, and leave everything else to programs that do them better. For these people – and to be fair they are the majority – Vim is the most popular choice.
Vim stands for Vi IMproved, as it is a more advanced version of Vi, which is an old Unix editor created by Bill Joy and others in the mid-1970s. It predates Emacs by quite a few years, and this is immediately apparent because it’s much less complex. Even Vim, which adds many features, is still much smaller and simpler than Emacs. However, on the flipside Vim’s age means that you will always find it (or Vi) installed on virtually any Unix box you come across.
To get started, type ‘vi’ at the command line. Most Linux distros have this symlinked to Vim, so you should be ready to go. If you don’t have Vim installed, it will almost certainly be available through your package manager.
Basic commands
Once you’re in Vim, you can move around using the cursor keys. Officially, the correct method is to use the H, J, K, and L keys to navigate left, down, up, and right. The programmers claim this is faster, but you should be fine with the cursors unless you find yourself connecting from a remote terminal that doesn’t handle them well.
When you first start Vim, you will be in ‘normal’ mode, that is, you can navigate around with the cursors and execute commands. If you want to quit Vim, press ‘:q’. That will quit Vim and return you to the command line.
To return to Vim, type ‘vim myfile.txt’ to also open the myfile.txt file for editing. If the file doesn’t exist, Vim will create it for you. Now, hit ‘i’ to enter Insert mode – this allows you to enter text, so go ahead and type freely. When you’re done, press Escape to go back to Normal mode, and you will be able to navigate again using the cursor keys. To delete a letter, move the cursor onto the letter you don’t want, then press Delete (not Backspace) to zap it. If you try quitting Vim now, you will see that it warns you that the file has been changed. You need to tell it to write or quit without saving.
To write a file, execute the command ‘:w’. To quit without saving, type ‘:q!’. Alternatively, if you want to write then quit, use ‘:wq’. As you can see, this essentially lets you combine two commands together; this is a recurring theme in Vim. While in Vim, you can undo your last command by pressing ‘u’. Vim keeps a long undo trail for you, so you can undo multiple changes simply by tapping the key several times.
There are a few supplementary navigation commands. Page Up and Page Down work as you would expect, but you can also use ‘$’ (go to end of line), ‘^’ (go to start of line), ‘w’ (go to start of next word), and ‘e’ (go to end of next word).
Amending text
As with writing and quitting, you can use the supplementary navigation commands mixed with other commands. For example, Vim’s ‘delete’ command is ‘d’. You can press it twice to delete a line. However, you can use ‘d$’ to delete from the current cursor position to the end of the line, or ‘dw’ to delete the current word only.
This becomes even more powerful when you add numbers into the equation. For example, ‘dd’ deletes one line, but ‘5dd’ deletes five lines, ‘500dd’ deletes 500 lines, and ‘5dw’ deletes the next five words. You can even use numbers plus the cursor keys. For instance, ‘5 | Up’ moves five lines up. The easy way to remember this is that you first specify the number, then the action, then the object of the action, so ‘5dw’ specifies the number 5, the action ‘delete’, and the object to delete is ‘word’.
Along with ‘i’ for Insert mode, you can use ‘a’ for Append mode, which does largely the same thing. However, ‘A’ (uppercase), puts you into Append mode and shifts the cursor to the end of the line. You can also use ‘R’ (uppercase) to enter Replace mode, which overwrites text as you type. Again, use Escape to return to Normal mode.
If you use ‘:w’ by itself, it will save the file with the current name. You can change that by using ‘:w somefile.txt’, or also ‘:wq somefile.txt’, if you want to quit at the same time. To copy text to the clipboard, use ‘yy’. This copies the current line, but you can also use ‘2yy’ to copy the current line and the next, or ‘100yy’ to copy the current line and the next 99. To paste, use ‘p’, or ‘10p’ to paste ten times.
Search, replace, and more
Search and replace in Vim uses Unix-style regular expressions that will be familiar to any experienced Perl programmer, although it shouldn’t prove too troublesome for the beginner.
To search, type ‘/’ then your criteria, and hit Enter. By default this will highlight all matches, and move the cursor to the first one. Navigate to the next match by typing ‘/’ and hitting Enter. To do a reverse search, use a question mark, eg: ‘? mysearch’, again using just the symbol to repeat a search.
To replace rather than just search, you need a more complex construct. To replace one instance of ‘search’ with ‘replace’ on the current line, you would use ‘:s/search/replace’. To replace another, just use ‘:s’ by itself. If you want to replace all instances on the current line, add ‘/g’ to the end of the command. The following: ‘:s/search/replace/g’ will replace all instances of ‘search’ with ‘replace’ on the current line. To replace all instances across the entire file, use ‘:%s’. So typing ‘:%s/search/replace/g’, or optionally add ‘c’ after the ‘/g’ will have Vim prompt you to replace each individual match.
You can execute any shell command from Vim using ‘:!’ followed by your command, so ‘:!ls’ would print a directory list in the current directory (where you were before launching Vim). You can even use ‘:!vi’ to launch another Vim inside your existing Vim, but that could get confusing. Alternatively, use ‘:shell’ to launch a full shell.
The last thing to show off is a unique feature in KDE, which allows you to use Vim as the default text editor for applications that support it. For example, both KWrite and KMail allow you to choose the text editor you want to use for the text, and through the KDE Control Centre you can configure Vim. You will need to make sure you have either KVim or GVim installed (some distros, like Fedora, call it ‘XVim’).
Next, go to KDE Control Centre, and select the ‘KDE Components’ list, and the ‘Vim Component Configuration’ option. You may need to point it to your new Vim binary, which looks like ‘/usr/X11R6/bin/gvim’. Hit ‘Test’ to make sure it works. If so, you now only need to switch the default editor over to Vim. To do that, go to the ‘Component Chooser’ option in the same ‘KDE Components’ list, select ‘Embedded Text Editor’ from the list, then switch it to Vim. Done.
Although Vim is a great deal easier than its rival, Emacs, its focus on text editing means that you don’t need to press too many keys to work your way around it. Similarly, because there are no menus or other clutter, you get to see more of your text on screen at a time. If you’re having problems remembering Vim’s mnemonic commands, refer to the box ‘Vim cheat sheet’ to brush up.

