BSD DevCenter
oreilly.comSafari Books Online.Conferences.


FreeBSD Basics

Using the vi Editor





Let's say that I've sent a file to a pager:

more a_file

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!"

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

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, and type:

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

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 take a look at the set commands first, then create the file. From a vi session, type:

:set showmode

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.

Another handy 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:

:set wrapmargin=10


:set wm=10

If you would like to have each line numbered:

:set nu

If you decide that looks yucky, turn it off with:

:set nonu

In fact, any set command can be turned off by repeating it with no like so:

:set nowm=10
:set noshowmode

To see your current settings, type:

:set all

If you're unsure what each set command does, you'll find them in the vi man page if you do a search for unset:

man vi

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

Next Pagearrow

Sponsored by: