Continuing our Dig Into Unix series, we’ve now covered the absolute basics of launching Terminal.app, moving around the file system, looking at files with cat, and learning about commands with man. Now, I’d like to introduce you to the power of vi.
vi (pronounced vee-eye) is a text editor for the command line, and was originally written by Bill Joy in 1976. vi is now part of every single Unix system, including OS X. The version included with Leopard is vim, which stands for Vi iMproved. One of the major problems that vi has faced over the years is that it has a major learning curve. Every action that you take in vi you do with keyboard combos. Since there are so many actions that you can do, it takes awhile to get used to the different keystrokes, but once you do, they become ingrained in your fingers. From time to time, when typing in some other app, I find myself hitting the escape key followed by
hhh trying to move the curser.
To get started, open up Terminal.app and type
vi. This starts the editor in command mode. Type
i to enter insert mode, and then you can begin typing text into the file. Copy the text from this article and paste it into vi, regular CMD-C / CMD-V will work just fine. This will give you something to work with.
Now that you’ve got some text, it’d probably be good to save it. Hit the escape key to put the editor back into command mode, and type
:w newfile.txt. This will create a text file named
newfile.txt in your home directory. Press
i again, and you can begin typing, starting where you left off. Press escape again.
To move to any line of text in vi, you can press the number of the line you want to go to and
G. For example, to go to the first line, type
1G. If you type that and the characters “1G” show up on your screen, you are not in command mode, don’t forget to press escape first. To get back to the end of the file, you can just press
G by itself. That will bring you to the start of the last line in the file.
- To get to the end of a line, you can press
$, and your curser will jump to the end of the line.
- To go to the beginning of the line, you press
^. Now you can jump around the file.
- To move the curser one character at a time, use the four keys
h j kand
hmoves one character to the left,
jmoves one character to the right,
kto moves one character up, and
lmoves one character down.
- To open a line below, you can press
o, and to open a line above,
These basic text editing commands form the basis of the real strengths of vi, and the reason I continue to use it every day. But to really take advantage of the power of vi, you need to create a configuration file for shortcuts.
Exit out of vi and write the file to disk at the same time by typing
:wq! while in command mode. Now, create a new file, and open the file in vi for editing by typing
vi .vimrc. The .vimrc file is the configuration file vi reads when starting. The important thing to remember about this configuration file is that you can add any of the commands above into the file, including movement, inserting text, and opening lines, and then map those commands to a keyboard combo. If you are familiar with TextExpander, you’ll follow right along with this concept, but it’s even stronger than that.
We are going to create a single line of text in the file that looks like this:
map ,a 1GiJack Doe^MGuy in Charge^MACME Inc.^M111 Main St.^MAnytown, USA
^M^M^M^MDear Sir,^M^M^M^MSincerely,^M^M^MJohn Doe^[10G i
The line above has been separated into two lines to fit on the page. When you see
^M, that’s not really two characters, it’s only one. It’s the return key, captured in raw form in the config file for use in generating our commands. To capture it, you press Control-V, followed by return. The esc key is also captured above by pressing Control-V and then hitting the escape key. That creates the
^[ character above, and allows us to enter some of the vi commands above into the config. Enter in that line of text above, using the Control-V trick to capture the return and esc characters.
Now, save the new file by typing
:wq! in command mode, open up a new file with
vi file, and test out your new
,a shortcut. If everything went well, it should look just like the video below.
The learning curve with vi is steep, but as with most things worth learning, the payoff is significant. This short article has barely scratched the surface of what vi can do. vi can be a full-fledged IDE, or you could use it to write your next book. The mapping features above are especially helpful if you do any programming, or if you manage any Unix or Linux servers. If you’d like to learn more, I’d suggest starting with
man vi, and maybe picking up a copy of the O’Reilly book “Learning the vi and Vim Editors.” Of course, I’d be happy to help out as well.
Back when I was first learning Unix, I asked an old graybeard why I should bother learning such a complicated, ancient text editor. He told me that since everything in Unix is a file, and almost all files in Unix are text, I needed some way to edit those files to control the system. Someday, he said, I would find myself without any other text editor, and without any other way to change a file. He was right, since I’m now working as a Unix systems administrator, I’ve been in that position for several years. Over time, I’ve come to appreciate all that vi can do for me.