By Andrew Min
Ah Vim. You can’t live with it and you definitely can’t live without it. It’s the world’s most powerful (and arguably the most popular) text editor, yet it’s virtually impossible to understand. You start typing, and nothing happens. You look for the save button, and it isn’t there. You try and exit, and Ctrl-C won’t do it. Fear not! Vim is actually quite easy, once you know a few basics.
First, what the heck is Vim?
The first thing you need to learn is what Vim actually is. Before you do that, however, you need to know what vi is. vi is a text editor written by Bill Joy (co-founder of Sun Microsystems) while he was at Berkeley. It quickly took off, becoming the undisputed champion of terminal-based text editors until Emacs came along.
But why is this article called The Dummies’ Guide to Vim? Well, in 1991, Bram Moolenaar created an extended version of vi for the Amiga platform. He chose the name Vim (Vi IMproved), included a lot of extra handy features, and made it cross-platform (vi only worked on UNIX). Soon, many people were choosing Vim over vi. Additionally, Vim releases new versions quite often while vi’s last release was back in 2005.
Most Linux and UNIX users have Vim or vi installed by default. Either one will work with this tutorial, though I suggest using Vim for its more advanced features if you have a choice. Windows, OS/2, Amiga, and OS X users can install it using the instructions at the download page. Make sure you install the command-line version of Vim (Windows users, this is the Win32 console executable), not the GUI version known as gVim.
Editing Documents in Insert Mode
The most important (and arguably the hardest) thing to do with Vim is edit a file. First, open up a terminal (gnome-terminal, Konsole, xterm, DOS prompt, Command Prompt, or OS X Terminal just to name a few) and run vim (or vi if you’re using vi).
After vim launches and starts a new document, hit the I key to go into Insert mode. This is the mode that acts just like a regular GUI text editor. The ~ represents a line that is not present in the document. Try typing a few lines to make sure you’ve got the hang of it.
So far so easy, right? Well, now it’s time to save it. However, you can’t do that in Insert mode, so exit it by hitting the Esc key. Esc will bring you out of whatever mode you are currently in, a handy feature to remember if you accidentally go into some bizarre mode. Now, hit the : key. That tells Vim that you’re about to execute a command. Then, type w ~/yay.txt (in Windows, this command won’t work, so run w yay.txt to save yay.txt in the same folder) and hit enter. This tells vim to write to ~/yay.txt. To make sure it saved, open another terminal and run cat ~/yay.txt. You can also go into a file manager and double click on the yay.txt file in the ~/ (home) folder (or in Windows, the folder where you installed Vim).
Wunderbar! You now know how to create a new file. Of course, that’s only half of it. You also need to know how to open an edit a previously saved file. To do this, run :q to quit Vim. That should take you back to the terminal. Now, run vim ~/yay.txt. The file you just saved (you did just save it, right?) should pop back open. If you want, you can also open a file from within vim with the :e filename command.
Editing Documents in Normal Mode
Normal mode won’t let you type, but you can actually do quite a few important things in it. Before you learn them, however, you’ll need to learn how to move the cursor in Vim. The keyboard that vi was programmed with had a left arrow on the H key, a down arrow on the J key, an up arrow on the K key and a right arrow on the L key. Therefore, the H key moves the cursor left, the J key moves the cursor down, the K key moves the cursor up, and the L key moves the cursor down. Here’s how to remember if you’re an American. Hawaii is west, Jamaica is south, Kalamazoo is north, and London is east. Simple geography. If you get really confused, just use the + and - keys to go up and down and just remember that H is the farthest to the left on an ergonomic keyboard and that therefore L has to go right. If you get really, really confused, you can use the arrow keys (wimp!).
There are other handy keyboard shortcuts as well. The { and } keys ([ and ] with Shift held down) will let you skip to the next paragraph. You can also move forward and back with the W and B keys. And the 0 (as in, the number) and the $ will let you move to the beginning and end of the current line.
There are also a few commands that are extremely useful when programming code. You can search for a pattern by running /pattern, replacing pattern with some pattern. For example, typing /Hello and hitting enter will take the cursor to the first hello. The ?pattern command does the same thing, but in reverse. And then there’s my favorite programming command, the % command, which moves your cursor to the matching bracket or brace that your cursor is currently on (that is, (), [], and {}).
Conclusion
You now know how to edit and create documents in vim, plus navigate around fairly well. And you still haven’t even scratched the surface. There are many more tips and tricks in the official user manual (warning: this is often considered heavier reading than War and Peace and the mplayer man page put together) with vimtutor (a UNIX program, not a Vim command) or the Vimdoc online manual. You can also try messing around with gVim, the afore mentioned graphical version of Vim that is often packaged together with its command-line counterpart, and Cream, a set of scripts to make Vim more powerful and more easy to use.




1 comment so far ↓
[…] The Dummies’ Guide to Vim (or, Vim for Complete Idiots) Dawning Valley - […]
Leave a Comment