- vi -

vi (pronounced vee-eye), is one of the most common UNIX editors, in fact many systems use vi as their default editor, and so it is therefore essential to know something about vi, even if it's just enough to recognise you are in vi, and how to exit the program. If you wish to know more about vi, then this tute will cover that as well.

The first thing you need to know about vi, is that unlike pico, which uses special control characters to perform commands, vi uses a command mode for performing commands, and an insert mode for inserting text. vi starts in command mode by default, however if you find yourself in insert mode, just hit the Esc key to return to the command mode.

The following commands are all used while in command mode.

Opening Files

When you want to open a new file in a directory, give a new filename with the vi command. For example, if you wanted to open a new file called filename in the current directory, you would enter

hostname:bugsy> vi filename
Since this is a new file, the buffer is empty and the screen appears as follows:

~
~
~
~
"filename" [New File]
      

The tildes (~) down the side of the screen indicate that there is no text in the file, not even blank lines. The prompt line (also called the status line) at the bottom of the screen echoes the name and status of the file.

To open an existing file, just type vi filename, where filename is the name of the file you wish to edit.

Exiting a File

You can quit working on a file at any time, save your edits and return to the UNIX prompt. The vi command to quit and save edits is ZZ.

NOTE: that ZZ is capitalized.

Typing :w will save your file but not quit vi; Typing :q will quit if you haven't made any changes to the file, and typing :wq will quit and save your file. (:wq is equivalent to ZZ).

If you wish to undo all of the changes you have made to a file since you last saved it, typing :e! will do this.

To quit vi without saving any changes, you need to type :q!

Inserting Text

In order to enter text into your file, you will need to change from command mode to insert mode. There are several ways to enter insert mode in vi.

i - Insert text before the cursor.

a - Insert text after the cursor.

I - Insert text at the beginning of the current line.

A - Insert text at the end of the current line.

o - Open a new line for text below the current line.

O - Open a new line for text above the current line.

NOTE: To exit insert mode, and return to command mode hit the esc key.

Moving the Cursor

Once you've added some text to a file, you may wish to change, delete, or copy some of the text you've entered. In order to be able to do this, you need to be able to move to the text you want to work with as quickly as possible. The following movement commands will allow you to do this.

h left, one character
j down, one character
k up, one line
l right, one line
w or W forward, one word
b or B backward, one word
e or E end of word
), ( Beginning of next, previous sentence
{, } Beginning of next, previous paragraph
]], [[ Beginning of next, previous section
O, $ First, Last position of the current line
+, - First character of next, previous line
n Column n of current line
H Top line of screen
M Middle line of screen
L Last line of screen
nH n (number) of lines after top line
nL n (number) of lines before bottom line
ctrl-F Scroll forward one screen
ctrl-B Scroll backwards one screen
ctrl-D Scroll down one-half screen
ctrl-U Scroll up one-half screen
ctrl-E Show one more line at the bottom of the screen
ctrl-Y Show one more line at the top of the screen
z return Reposition the line with the cursor to the top of the screen
z. Reposition the line with the cursor to the middle of the screen
z- Reposition the line with the cursor to the bottom of the screen
ctrl G Display the current line number
nG Move to line number n
G Move to the last line in the file.
:n Move to line number n
Searches

The following commands are used to conduct searches.

/pattern Search forward for pattern
?pattern Search backward for pattern
n, N Repeat last search in the same, opposite direction
/, ? Repeat previous search forward, backward
fx Search forward for the character x in the current line
Fx Search backward for the character x in the current line
tx Search forward for the character before x in the current line
Tx Search backward for the character before x in the current line
; Repeat previous current-line search
: Repeat previous current-line search in the opposite direction
Changing Text

You can replace any text in your file using one of the change commands below.

r Replace the character with the character typed next.
~ Change the case of the character
cm Change text block defined by the movement command m (eg cW)
cc Change the current line
C Change to the end of the current line
R Type over characters
s Delete character and substitute text
S Delete the current line and substitute text
Deleting and Moving Text

To delete text, use one of the delete commands below.

x Delete character
X Delete the character before the cursor
dm Delete the text block defined by the movement command defined by m(eg, d/pattern or d G)
dd Delete the current line
D Delete to the end of the current line

You can move text by first deleting it, and then pasting it in the new location using one of the following commands

p, P Paste deleted text after, before the cursor
"np Put text from delete buffer n after the cursor (for the last nine deletions)
Copying and Pasting Text

vi uses the term "yank" to describe the act of copying text. Therefore to copy text you would use one of the following commands

ym Yank (copy) text block defined by the movement command m (eg y]] or y})
yy, Y Yank the current line
"ayy Put current line into a buffer named a

Pasting the text to a new location is done using the following commands

p, P Paste deleted text after, before the cursor
"ap Put text from the buffer named a before the cursor
Other Useful Commands
Here a couple of other commands you may find useful.

. Repeat the last edit command
u, U Undo the last edit, restore the current line

NOTE: Most vi can be preceded by a count. For example 5w moves forward five words, and 7dw deletes from the cursor position to the start of the 7th word forward.

For further information on vi a good book to read is Learning the vi Editor by Linda Lamb.



Page Created 14th February 2005.
Last Modified 1st April 2005.