Front | Info | Lists | Newsfeeds | Study Guide | What is BSD?
Advertisement: The OpenBSD PF Packet Filter Book: PF for NetBSD, FreeBSD, DragonFly and OpenBSD

BSD Links
·New Links
·User Groups

This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at

Be familiar with the vi(1) editor


The default editor on BSD systems is often vi(1) and many system utilities require familiarity with vi(1) commands. Be able to edit files using this editor, as well as modify a read-only file or exit vi(1) without saving any edits to the file.


The vi editor is a screen oriented text editor. ex is a line oriented text editior. Both ex and vi are different interfaces to the same program.

FreeBSD, NetBSD, OpenBSD and DragonFlyBSD all use the nex/nvi versions of the ex/vi text editors, these are bug-for-bug compatible replacements for the original Fourth Berkeley Software Distribution (4BSD) ex and vi programs.

As stated above vi is a screen editor, in practice this means that it takes almost the entire screen. The sreen is mainly a display of the lines in a file, except for the last line which is used for you to give commands to vi. vi is a modeful editor. This means that you are either entering commands or entering text. You need to be in the correct mode to do one or the other.

vi commands can be broken down into four types of commands:

  1. Command-Line options
  2. Movement Commands
  3. Editing Commands
  4. Exit Commands

To ease the process of familiarisation with vi, it is important to remember that it is a modeful editor. When starting vi from the command line you are initially in command mode. In command mode typing on the keyboard issues commands to the editor. These commands usually one or two characters, such as i to insert text or cw to change a word. To enter text, you need to be insert (i) or append (a) mode. To return to command mode you press the Esc key.

To remind you which mode you are in you can use :set showmode to tell vi to display on the command line at the bottom of the screen which mode you are in.

Command-Line Options

The command-line options are those used to invoke the vi editior, such as starting vi to edit a file called filename. If the file does not exist it will be created, thus if you edit a file with vi and it opens an empty file then chances are you have mistyped the file name.

$ vi filename        This invokes vi on filename

If you wanted to invoke vi in read-only mode you have two options, either invoke with the -R switch, or use view:

$ vi -R filename     Open filename read-only

$ view filename      Open filename read-only

All the switches are documented in the man page. But two useful switch when invoking vi are + and +n as illustrated below:

$ vi + filename      Open filename at last line 

$ vi +n filename     Open filename at line number n

Exit Commands

Before dealing with the movement and editing commands we will look at the exit commands, the reason for this is that if you open a file by mistake or wrongly edit a file it is useful to know how to recover the situation.

To that end the first exit command to mention is:

:q!              exit vi forcing all edits to be thrown away.

This is important as vi normally tries to save all edits to files. If you just use :q which is quit file in vi you will be warned if modifications have been made.

:q               quit file

To save the file you use :w and this can be combined with the quit command to write and exit the file:

:wq             write (save) and quit file

The ZZ command performs the same function as :wq in that the file is saved, if modified, and then vi quits that file.

If you are in command mode typing i or a with put you in editing mode. i inserts text before the cursor and a inserts text after the cursor. To exit editing mode you just press the Esc key. Once you have started editing a file it is useful to use the showmode command, this will tell you if you are in command or edit mode:

:set showmode

This will put either the words Command, Insert or Append on the right hand side of the command line at the bottom of the vi window, and reminding you which mode you are in.

Movement Commands

vi provides many methods of moving around the file, from character level to moving by screens, and using search to move around the file. This section will give a brief overview of the many movement commands that are available in vi. Character level movement is achieved with:

h    Left
j    Down
k    Up
l    Right

Character level movement can also be achieved using the arrow keys on the keyboard. Moving around a file at the character level is not always the most efficient method, however you can backwards and forwards through the file by word beginnings and endings:

w or W   move forward by word
b or B   move backwards by word
e or E   move forward to the end of a word

At the line level:

0   first position of current line
$   last position of current line
^   first non-blank character of current line
+   first non-blank character of next line
-   first non-blank character of the previous line

Movement through the file can also be achieved by line numbers, where Ctrl-G will display your current line number. nG will move you to line n, and G will move you to the last line of the file. You can also use the ex command for moving to a particular line by doing :n where n is the line number to move to.

Before leaving the section on movement around files, another useful way to move around a file is using vi search capabilities. By typeing /pattern or ?pattern vi will search forwards (/) for pattern or backwards (?) through the file. Similarly to repeat the previous search just entering / for a forward search and ? for a backwards search. n will repeat a search in the same direction, and N will repeat the search in the opposite direction.

Editing Commands

vi editing commands cover inserting text, changing text, deleting or moving text and yanking (vi version of copying) text. i and a have been mentioned above. When they are capitalized you get:

I insert text before beginning of line
A insert text after end of line

Changing text can be achieved at the character, word, line and even greater levels.

r   replace character under cursor
    e.g. typing rt would replace the character with a 't'
cw  change the word
cc  change current line
C   change to end of line
R   overwrite characters
s   delete character and insert new text
S   delete current line and insert new text

To copy words or lines in vi they are Yanked using:

yw  yank a word
yy  yank current line

Yanked words can then be pasted using:

p  put yanked text after cursor
P  put yanked text before cursor

The deletion commands in vi, can also be used to move text around a file as vi puts the deleted text in a buffer which can then be put elsewhere in the file by using the p and P commands.

dd  delete current line
dw  delete word
x  delete character under cursor
X  delete character before cursor

Finally there are four other editing commands that are worth a quick mention:

.  repeat the last edit command
J  join two lines together
u  undo last edit
U  restore current line


:w     write (save) file
:wq    write (save) file and quit
:wq!   write (save) file, quit and ignore warnings
q!     exit vi forcing all edits to be thrown away
dd     delete line under cursor
y      yank line under cursor (save to buffer)
p      put buffer in current cursor position
x      delete character under cursor
i      enter insert mode
a      enter append mode (insert after cursor)
/      enter search mode 
          /pattern would search forward for pattern
:      enter ex command
:r     read file into current cursor position
          :r filename would insert the contents of filename into current file
ZZ     save and exit vi
:set number  display line numbers
:set list    display line end (displays $ at the end of each line)

Practice Exercises

  1. Starting the editor

  2. Getting out of the editor

  3. Moving around in the file (including Arrow keys)

  4. Making simple changes

  5. Writing, quitting, editing new files

More information

vi(1) including: :w, :wq, :wq!, :q!, dd, y, p, x, i, a, /, :, :r, ZZ, :set number, :set list

ex, Bill Joy. USD/

FreeBSD Manual Page for vi

NetBSD Manual Page for vi

OpenBSD Manual Page for vi

DragonFlyBSD Manual Page for vi

Learning the vi Editor by Linda Lamb & Arnold Robbins is a useful text from O'Reilly. In addition there are many tutorials available on the internet.

Vi Security

While using the vi editor you can escape to a shell, using :sh, or execute comands using :! cmdname, thus if you allow users to edit configuatation files using sudo, you might well be giving them root access.

Vi Clones

There are many vi clones that add functionality, such as Vim and Elvis The advantage of these clones is that they often have GUI's and run on other OSes so you can use for favourite editor (vi of course ;~D) where ever you go.

Front | Information | Lists | Newsfeeds