howtos‎ > ‎

vi essentials

The vi editor is a standard part of almost every unix or unix-derived operating system. While it is unfriendly to the beginner, it may be the only choice in some environments.

This page is for occasional vi users — it includes the essential steps to open a file, make some changes and exit. (Note that these are the lowest-common-denominator commands - your vi will probably be better than this, but it shouldn't be worse.)

getting in

To edit a file, type

vi filename

at the shell prompt. If you don't supply a filename, vi will open with an empty file.

The vi screen looks like this:

Some text in the file. Some text in the file. 
Some text in the file. Some text in the file. 
~
~
~
~
~
~
~
~
~
~
~
filename: unmodified: line 1

Existing text (if any) is at the top. The lines beginning with ~ are empty space after the end of file. The bottom line is for messages and prompts.

modes

Unlike most editors, vi doesn't use control keys or menus for editing text — many of the commands in vi are single letters. Since these are the same keys used for typing text, vi operates in 2 modes.

When you first start vi, it is in command mode. In this mode you can move around, search, edit and do anything except actually type text into the file.

To type text, you put vi into insert mode. In this mode, you have the functionality of a typewriter and not much more.

Since command mode is the default, most users have an unpleasant first experience with vi — confronted with an empty screen, they start typing only to produce a lot of error messages or a scrambled text file.

If you're not sure which mode vi is in, press escape (or control-[ on old terminals). This will put you into command mode, or beep if you were already there.

moving around

In command mode, the first thing you probably want to do is move around the file. Since vi predates the PC keyboard by a decade or so, it doesn't generally use the cursor keys such as home or pageup (newer operating systems may be an exception).

Usually, the most you can hope for is working arrow keys. On old or badly configured terminals, even the arrow keys may not work. In that case, you'll need the following keys.

h cursor left
j cursor down
k cursor up
l cursor right

The next keys navigate by 'strict' words: alphanumerics and punctuation are treated separately, which is handy for code.

w word forwards
b word backwards
e end of word

These keys navigate by 'text' words: anything except whitespace is treated as a word.

W word forwards
B word backwards
E end of word

These keys navigate by paragraph, which is any text separated by empty lines.

{ previous paragraph
} next paragraph

These keys navigate by C function, which is any text starting with a left brace in the first column. If none is found, they will jump to the start and end of the file.

[[ previous function
]] next function

To scroll by screen, use these keys.

control-b screen backwards
control-f screen forwards

All the keys mentioned so far can take a repeat count beforehand. This can save you a lot of typing.

80k 80 lines up
3w 3 words forward
5{ 5 previous paragraphs

Finally, some keys for line movement.

^ start of line
$ end of line

searching

You may want to search the file for a particular string.

/ match expression

Because it needs a pattern to search for, this command does not run immediately. Instead, it prints a prompt on the bottom line and waits for you to enter a regular expression, then press enter.

Regular expressions are a pattern matching language used by many unix tools. The simplest form is a plain text string which will be matched literally.

To repeat the search, use these keys.

n next match forwards
N next match backwards

You can also match more complex patterns with these commands.

inserting text

Once you've found the right place, it's likely you'll want to insert some text. If you've opened an empty file, this will be the first thing you do.

Since vi starts in command mode, you need to get into insert mode where you can just type. Depending on where you want to put the text, there are a few different ways to do this.

i insert text before cursor
a append text after cursor
o open line below cursor

In an empty file, you'll probably start with i.

There are alternative versions of these keys that are also quite handy.

I insert text before start of line
A append text after end of line
O open line above cursor

You can't enter commands while you're inserting text, but there are a few editing keys in this mode.

backspace undo last character
control-w undo last word

To exit insert mode, use escape. This will put you back in command mode.

changing text

Instead of inserting text, you may want to replace something that's already there. The change command is a little more complex.

c [count] movement

This command combines with the movement keys discussed previously. Instead of moving the cursor, they tell vi how much text to change.

c3w change next 3 words
c5{ change previous 5 paragraphs
c$ change to end of line

When you use this command, vi switches into change mode. This is like insert mode, but it overwrites existing text. To show this, vi puts a $ character at the end of the text to be replaced.

Once you're done, press escape. The old text will disappear, replaced by what you just typed.

Use this command to replace whole lines.

cc change current line
5cc change next 5 lines

If you only want to change one character, there's a simpler command.

r replace character

This will overwrite a single character and put you straight back into command mode.

cutting & pasting

The commands to delete (cut) and yank (copy) work the same way as the commands to change text.

d [count] movement
y [count] movement

d3w delete next 3 words
y5{ yank previous 5 paragraphs
d$ delete to end of line

Use these commands to work with whole lines.

dd delete current line
yy yank current line

And these for single characters.

x delete character
X delete previous character (backspace)

Once you've collected some text, move to the new location and paste it.

p paste text after cursor
P paste text before cursor

undo & repeat

vi can undo or repeat the last command that modified text.

u undo
. repeat

The 'last command' includes inserts or changes — if you just inserted some text, you can move elsewhere and repeat the typing with . as long as you don't edit anything else in between.

You can also undo all changes made to the current line.

U undo current line

getting out

These commands use the colon prefix; like the search command, it prints a prompt and waits for you to complete the command and press enter.

To write the current file, use this command.

:w

To write it to a different file, add the filename.

:w filename

This command will fail if the file is readonly; you can 'force' the write by putting an exclamation mark after the command.

:w!

To exit vi, use this command.

:q

If the current file is has been modified, vi will print an error message. To quit without saving, force the command.

:q!

To edit a different file without exiting, use this command.

:e filename

Since vi only handles one file at a time, this command behaves like quit; it will fail if the current file has not been saved.

Comments