Blog Post

Dig Into Unix: vi


Continuing our Dig Into Unix series, we’ve now covered the absolute basics of launching, 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 jjj or hhh trying to move the curser.

Getting Started

To get started, open up 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.

Navigating Lines

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 k and l. h moves one character to the left, j moves one character to the right, k to moves one character up, and l moves one character down.
  • To open a line below, you can press o, and to open a line above, O.

Configuration File

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.

Worth Learning

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.

13 Responses to “Dig Into Unix: vi”

  1. @carnz Hey thanks! I guess you learn something new every day, but I doubt that I’ll ever be able to actually *not* do wq! :)

    @Marto @archie Thanks for the kind words, I’m glad you liked the article.

    @HazardousPaste Ewhatzitmacs? Bah! vi is the one true text editor! :)

  2. Reclusive Monkey

    @Hieronymus: nano won’t always be installed.

    I read the same advice a long time ago, that vi will always be installed on any *nix system, and its probably the best advice I ever read.

    • You can, if it is on the system you are using. Nano isn’t on many unix systems by default. Often, vi is the only editor installed. Odds are that some day you will be using a unix system and the only editor at your disposal is going to be vi (Gentoo, AIX and others). Learning the basics of vi is a good idea for anyone who wants to use unix. As a unix system administrator, I can speak from experience and I can tell you that I am glad I learned vi. And we should mention that while you’re in vi, simply issue :h and hit enter to view the help. Of course, Google is also a good resource.

  3. I really appreciate this. I’m working in Civil Engineering and messing about with Unix/Linux/MacOSX at home in my spare time, however I can see a career change in my future and sys admin is the way to go so I’m trying to gleem as much from free sources as I can so this article is right up my street.

    Cue obvious question from Noob but if anybody feels the urge to point me in a particualr direction to aquire more of these skill’z I’d be more than happy.

    Thanks in advance.

  4. archie

    Hey Jon, you brought a smile on my face! Nice writing.
    Personally I recall a time i didn’t use vi for 2 or 3 years. When I was using it again on a document. My eyes saw what needed to be done and my fingers did it. I was left bewildered about what my fingers did exactly. Vi really sticks to your fingers as you describe and boy, what a steep learning curve it has!!