vi Basics

vi Basics

The uses and benefits of any editor like vi are almost religious. Often, the reasons people choose one editor over another are purely a matter of personal taste. Each offers its own advantages and functionality. Some versions of UNIX provide other editors, such as emacs. However, the nice thing about vi is that every dialect of UNIX has it. You can sit down at any UNIX system and edit a file. For this reason more than any other, I think it is worth learning.

One problem vi has is that can be very intimidating. I know, I didn’t like it at first. I frequently get into discussions with people who have spent less than 10 minutes using it and then have ranted about how terrible it was. Often, I then saw them spending hours trying to find a free or relatively cheap add-on so they didn’t have to learn vi. The problem with that approach is that if they has spent as much time learning vi as they did trying to find an alternative, they actually could have become quite proficient with vi.

There is more to vi than just its availability on different UNIX systems. To me, vi is magic. Once you get over the initial intimidation, you will see that there is a logical order to the way the commands are laid out and fit together. Things fit together in a pattern that is easy to remember. So, as we get into it, let me tempt you a little.

Among the “magical” things vi can do:

  • Automatically correct words that you misspell often
  • Accept user-created vi commands
  • Insert the output of UNIX commands into the file you are editing
  • Automatically indent each line
  • Shift sets of lines left or right
  • Check for pairs of {}, () and [] (great for programmers)
  • Automatically wrap around at the end of a line
  • Cut and paste between documents

I am not going to mention every single vi command. Instead, I am going to show you a few and how they fit together. At the end of this section, there is a table containing the various commands you can use inside vi. You can then apply the relationships to the commands I don’t mention.

To see what is happening when you enter commands, first find a file that you can poke around in. Make a copy of the termcap file (/etc/termcap) in a temporary directory and then edit it (cd /tmp; cp /etc/termcap . ; vi termcap). The termcap file contains a list of the capabilities of various terminals. It is usually quite large and gives you a lot of things to play with in vi.

Before we can jump into the more advanced features of vi, I need to cover some of the basics. Not command basics, but rather some behavioral basics. In vi, there are two modes: command mode and input mode. While you are in command mode, every keystroke is considered part of a command. This is where you normally start when you first invoke vi. The reverse is also true. While in input mode, everything is considered input.

Well, that isn’t entirely true and we’ll talk about that in a minute. However, just remember that there are these two modes. If you are in command mode, you go into input mode using a command to get you there, such as append or insert (I’ll talk about these in a moment). If you want to go from input mode to command mode, press Esc.

When vi starts, it goes into full-screen mode (assuming your terminal is set up correctly) and it essentially clears the screen (see the following image). If we start the command as

vi search

at the bottom of the screen, you see

“search” [New File]

Your cursor is at the top left-hand corner of the screen, and there is a column of tildes (~) down the left side to indicate that these lines are nonexistent.

In the image below we see a vi session started from a terminal window running under X-Windows. This is essentially the same thing you will see when starting vi from any command line.

Image – Main vi window. (interactive)

As with most text editors or word processors, vi gives you the ability to save the file you are editing without stopping the program. To issue the necessary command we first input a colon (:) when in command mode. When then press w (for write) and the press the enter key. This might look like the following figure:

Image – Writing a file in vi.

After you press the enter key, you end up with something like the following image:

Image – Writing a file in vi. (interactive)

If you are editing a file that already existing and try to save it like this, you may get an error message that says the file is read only. You will also get this message, when trying to save a file from “view”, which is the “read-only” version of vi. To force the file to be written, you follow the w with an exclamation mark: :w! .

The ex-mode (or command mode) also allows you to do many other things with the file itself. Among them are

  • :q to quit the file (:q! if the file has been changed and you don’t want to save the changes)
  • :wq to write the file and quit
  • :e to edit a new file (or even the same file)
  • :r to read in a new file starting at the current location