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.