Vi, pronounced “vee-eye”, is a full-screen text editor available in UNIX. Vi allows you to view and edit text files and is generally one of the faster editors in terms of speed of work. Vi is the oldest and most compact of a series of editors that use the same interface and commands. Vi provides the user with little guidance and requires that the user knows a number of obscure keystrokes.

Other editors compatible with the interface explained in this document are vim and gvim. gVim is the graphical version of vim, and includes menus and mouse
control to make the interface simpler to use. It requires X-Windows compatibility (explained in the gVim section), and is the suggested vi editor for new vi users.

Starting Vi

Vi is invoked at the UNIX prompt by typing vi filename. If the file exists, its contents are placed in the screen. If the file does not exist, a screen appears filled with ~ characters along the left side.

The Command and Edit Modes

Vi operates in two modes, command mode and edit mode. When vi starts it is in command mode. In this mode you can move around using the keys j (down),
k (up), h (left), and l (right). In command mode the colon allows you to type in additional commands.
By typing : you will be taken to the command line at the bottom of the screen. From the : you can issue a number of commands including wq which writes the file you are editing and then quits vi.

Command Set

NOTE: n will repeat the last search, whereas <n> represents a number to be typed in that generally sets the number of times a command is repeated. The <n> is optional, and is assumed to be 1 if not present. For example the keystroke 7x will delete 7 characters from the current cursor position.

In order to type in text you need to go into the edit mode. To start editing you can type a number of keys:

a Start editing after the current character (append)
i Start editing in front of the current character (insert)
A Start editing at the end of the line

To return to the command mode press the escape key

Moving Around in Vi

h or left arrow move left one character
l or right arrow move right one character
j or up arrow move up one line
k or down arrow move down one line
Control-d move down half screen
Control-u move up half screen
<n> Control-f scroll forward <n> screens
<n> Control-b scroll backward <n> screens
1G go to top of file
G go to bottom of file
<n> G go to line # <n>
0 move to beginning of the line
$ move to end of the line
<n> w forward <n> words
<n> b backward <n> words

Editing Commands

x delete a character
dd delete a line
r replace with next character typed
u undo last change
mx mark current line with an x (x can be any character)
'x return to line marked with x
d'x delete from line marked x to current line
y'x yank from line marked x to current line
<n> yy yank <n> lines starting at current
p paste yanked lines below current line
P paste yanked lines above current line

Leaving Vi

ZZ exit from vi saving changes
:wq exit from vi saving changes
:q quit from vi (only if no changes made)
:q! force quit from vi (without saving changes)


i insert at cursor
o open new line below current line
O open new line above current line
ESC exit insert mode (stop inserting)


<n> x delete <n> characters
<n> dd delete <n> entire lines
<n> dw delete <n> words


/pattern forward search for pattern
?pattern backward search for pattern
n repeat last search

Changing and Replacing

<n> cw change <n> words
<n> cc change <n> entire lines
:s/old/new/ replace old string with new string on current line (replaces first occurrence on line only)
:1,$s/old/new/g replace old with new globally (lines 1–$ means all lines and g means all occurences on a line.)


u undo last change
. repeat last change
J join current line and next line together
Control-g report line number information
Control-l refresh screen
:w filename write file out to filename
:r filename read in filename below current line