Using the vi Editor10/25/2001
Okay, I admit it. I like using the vi editor. In fact, I have fond memories of using vi when I was teaching myself C++ while playing Peter Gabriel at full blast. I never became highly proficient at C++, but I can still type and get vi's bell to go off in rhythm to any piece of music. I sorta miss that bell when I'm using another editor.
Everyone knows they should have a passing knowledge of vi, but most people groan at the thought of using it. In today's article, I'd like to demonstrate some of the tricks that vi has up its sleeve. It really is a powerful little editor and I'm constantly amazed at its number of built-in shortcuts.
One of the more recent tips I learned is from UGU's tip of the day. If you haven't heard of this great reference, you can read the archives or sign up to receive the daily tip.
Let's say that I've sent a file to a pager:
and I notice a typo as I'm reading through the file. If I type:
the vi editor will be invoked and the bottom of my screen will show the name of the file and the line number my cursor is on. I can then correct the typo. To save the correction and go back to the pager:
What could possibly be more efficient than that?
The vi editor comes with a built-in help system which is useful for learning new shortcuts. If you type
you'll see this at the bottom of your screen:
To see the list of vi commands, enter ":viusage<CR>" To see the list of ex commands, enter ":exusage<CR>" For an ex command usage statement enter ":exusage [cmd] <CR>" For a vi key usage statement enter ":viusage [key]<CR>" To exit, enter "q!" Press any key to continue [: to enter more ex commands]:
For example, if I think I remember that vi uses the letter "o", but I can't remember for what, I can type:
:viusage o Key: o append after line Usage: [count]o Press any key to continue [: to enter more ex commands]:
Or, if I feel the need to kill an afternoon, I can enter:
and practice using the shortcuts I'm not familiar with. While this is handy, it can be quite overwhelming when you're first learning vi. One of the neatest tricks I learned was how to make a customized help screen. I picked up this tip from Steve Moritsugu's "Using Unix" (ISBN 0-7897-1632-1). I like to create a file with about 10 commands I want to learn, and once I've mastered those, I'll edit the file with a new set of commands.
Let's create a help file for a beginner that shows the most basic commands:
vi /tmp/help echo " :q! abort without saving :wq write changes and quit a append text after cursor i insert text before cursor o open a new line below current line O open a new line above current line x delete character dd delete line 1p restore most recently deleted text at cursor u undo last change (repeat to undo undo) 10G go to line 10 (can use any line number) G go to last line 0 move to beginning of current line $ move to end of current line"
Let's start with those commands; the trick is to make a help file that
will fit on one screen. Don't forget the
echo command and the quotation
marks at the beginning and end of your file. Now, become the superuser,
ls -l /usr/bin/help ls: /usr/bin/help: No such file or directory
Ignore the error message, it's a good thing. Now type:
cd /usr/bin mv /tmp/help help chmod 755 help exit
To use the customized help screen while in a vi session, type:
Note that you have to remember to include the exclamation mark as you're really telling vi to execute a command you created called "help".
The vi editor also has quite a few
set commands. It's useful to try
them out first in a vi session and see which ones you like as you can
invoke them permanently by creating a vi configuration file. Let's
a look at the
set commands first, then create the file. From a vi
You'll note that a word appeared at the bottom right corner of your screen telling you what mode you are in. Some examples are Command, Insert, Replace, and Append modes.
set command allows you to turn on autowrap, meaning you
never have to remember to press "enter" as vi will wrap your long lines
for you. To set this option, type either:
If you would like to have each line numbered:
If you decide that looks yucky, turn it off with:
In fact, any set command can be turned off by repeating it with
:set nowm=10 :set noshowmode
To see your current settings, type:
If you're unsure what each
set command does, you'll find them in the
vi man page if you do a search for
man vi /unset
Finally, if you set a
set command during a vi session, it will be
lost when you quit your vi session. To permanently keep a setting,
create a file in your home directory called
.exrc like so:
vi ~/.exrc set showmode set wm=10
Include your favourite set commands, and when you're finished, save your changes:
Pages: 1, 2