ERODD HOME vitut.htm CPSC 341 Home

Using the LINUX/UNIX vi Editor
Self-Study Tutorial
Earl Rodd -

For general lab information, see General Unix Lab information.

vi - the basics

The most common editor in UNIX systems is 'vi'. While other editors (e.g. emacs) are widespread, most UNIX programmers and administrators use vi at least some of the time. Also, ANY UNIX system will have vi available.

Note: In our lab, there is advanced version of vi called "vim" which you may use. One helpful feature is that it always shows the line/column of the cursor in the bottom right hand corner of the screen. The "vi" command is actually an alias for "vim" in the lab so that you will normally be using "vim". This tutorial applies to "vim" as well as "vi".

To use vi, at the command prompt, type:

   vi filename

      or

   vim filename

      or

   view filename

"view" is a read-only version so that you do not accidentally corrupt a file such as a system configuration file.

In vi, you move around using normal keys such as pgup, pgdn, and cursor keys. ENTER should go to the next line. However, it is possible that a system has failed to configure these keys!

The trick comes when you want to change the file. In 'vi', you don't just overtype what you see on the screen.

To get started, it is easier to use some existing file rather than start by creating a new file. To try out the commands here, first copy a nice text file like:

cp /etc/profile myjunk
vi myjunk

The only way to LEARN vi is to USE it! So copy a file as noted above, and start trying to use the commands shown below!

'vi' has a command mode and a typing mode. When you enter the editor, you are in command mode. These commands consist of one or more characters. The command is perfomed when the key(s) are typed. DO NOT press ENTER after typing the command! Nearly every key on the keyboard is some command or another. Try these commonly used commands:

Extended keys available in vim

In our LINUX labs, we have an enhanced "vi" with these extra keys:

Next are "commands" which are typed and followed by the ENTER key. As soon as you type the ":" (colon) starting one of these commands, vi recognizes that you are typing a command and it appears at the bottom of the screen.

Next are a set of commands which put you into a typing mode such that you can then type data which goes into the file. You do NOT press ENTER after these commands. Examples are:

These are the basics. With these, you can edit programs and configuration files successfully!

vi - copy/move

The most used of more advanced functions is the ability to move text around in a file (e.g. copy or move from one place to another).

This is done by "yanking" line(s) and then "putting" them. To yank,
yy - yanks one line
5yy - yanks 5 lines
To put a copy of the lines somewhere else, go that place in the file and type:
p

To MOVE (i.e. copy and delete the original):

5yy  - yanks the lines
5dd  - deletes the lines
     - move cursor to new place
p
WarningDo not do other operations between the yy/dd and p commands! Other operations such as changing a line also use the same buffer as yy.

vi - copy from another file

To copy a set of lines from file 'old' to file 'new':

vi old
(take cursor to line from where you want to copy)
"a5yy - will yank 5 lines into buffer a
:e new - will now edit file new
(move cursor to where you want to copy the lines)
"ap - will copy the lines
Note: the 'a' in the above example is a buffer name. You could use any letter. This allows you to store mutiple sets of lines in the old file before copying them to the new file.

vi - marking

You can "mark" a place in a file so that you can easily go back there without the need to search for a string or go to a line number.
To mark a place in a file, move the cursor there and type:
ma - marks the spot and calls it 'a'
mb - marks the spot and calls it 'b' etc.
To go to that spot in the file, type:
`a - go to mark a
You can also delete all text down to a mark with:
d`a

vi - documentation

In general in UNIX, you would type 'man vi.' In our system, we are using an enhanced vi called 'vim'. 'man vim' will get the basic help. To get help on all the commands as noted above, just vi some file and type ':help'. This will vi a large help file. Just use ':q' when you are done.