The Vi Editor

Vi, the original UNIX screen editor is available on all UNIX and Linux systems in Cardiff School of Computer Science & Informatics. It is a powerful editor with a huge number of commands and options. However, it is sometimes considered hard to use. This page describes a few of the simple commands needed so that you can use vi to create files. It also gives a list of vi and ex (line mode) commands.

Vi can potentially run on any computer with a monitor or vdu screen. On Linux systems, vi is a mode of the even more powerful editor, vim. Vim is context sensitive. If it recognises the type of file it is editing it will highlight parts of the text in colour to show you features of the content. For example, if the file is a C program, it can highlight reserved words, strings, blocks and many more.

In general, the description here of vi also applies to vim in vi mode.

The editor vi can be used in terminal emulation windows on UNIX or Linux workstations, or on terminal emulation windows (like telnet or PuTTY or SSH Client) connected remotely to UNIX or Linux from other systems such as Windows PCs.

It is a terminal-screen based editor, i.e. part of file that's being edited is displayed in the terminal emulation window. You edit the file by editing the characters in the terminal. When you are happy with the edits, you write them away to the file. Up to that point, the file on disk is untouched.

Running Vi

Vi may be used to create or edit a file. Like all programs on UNIX and Linux you can start vi from the Shell by giving its name. Give the name of the file to be edited as an argument to the command. For example. to edit myfile.c use the following command.

vi myfile.c			# or vim myfile.c

If myfile.c exists it is read by vi. If myfile.c doesn't exist it is created. The terminal screen acts as a window on the file and as many lines as can fit into the terminal emulation will be displayed. To begin with the first few lines of the file are displayed. The cursor is positioned at the top left corner of the terminal.

Editing a New File

We will now see how to create a new file using vi. We will create a file called file1. Type the below command into a terminal window and the window will be redrawn as shown.

vi file1

Once you start to insert text, the vim version information will disappear.

Vi is a moded editor, i.e. it operates in one of two modes. The first is editing mode and the second is text mode. In editing mode, you can amend the contents of the file as shown in the terminal. In text mode you can add new text to the file. Since the file file1 is empty, we must first use text mode to type lines into the file.

The Append Command

When you start it, vi is in editing mode. The command a switches vi to text mode and allows you to append characters from the cursor's current position. After you type a, every character you type appears in the terminal and is added to the contents of the file. So typing aThis is some text. will add the characters This is some text. to the file.

To start a new line, just press the Return You can continue to type in text mode until you have finished inputing the text you want.

To leave text mode press Esc.

The Insert Command

The insert command i also switches the editor to text mode. Here characters are inserted in front of the current cursor position.

Correcting Mistakes

If you make a typing error while in text mode, you can use the delete or backspace keys to rub out characters you have typed in.

If you are outside text mode (i.e. in editing mode), the following commands can be used to amend the contents the edit buffer:

Command Description
x delete the character the cursor is on
X delete the character to the left of the cursor
dw delete from the cursor to the end of the word
D delete from the cursor to the end of the line
dd delete the line containing the cursor
r replace the character at the cursor by the next character you type
s substitute the character at the cursor with the string of characters you type (terminated by the Esc or Ctrl + [).

Moving About in a File

The arrow keys on the keyboard , , and can be used to move the cursor left, right, up or down when vi is in editing mode. If you try to move above or below the lines in the terminal window, the lines in the screen terminal are rolled down or up to reposition the window on the file.

You can also locate the cursor by context. The following command will move the cursor to the next occurence of string

/string

Similarly, the below command will move the cursor to the previous occurrence of string.

?string

There are also go to line n commands. For example, to move the cursor to line 12 of we would use the following. To move the cursor to the end of the file we can use :$.

:12

Ex Commands

Commands preceded by a colon, such as those above, are called ex commands. This is because they are line-oriented commands used by the ex line-oriented editor. You can invoke ex from the shell with the command:

ex filename

But, more usefully, you can give ex line-mode commands to vi by preceding them with a colon (:). All line-mode commands are terminated by pressing the Return key. One particularly useful line-mode command is the substitute command s. For example:

:s/hello/goodbye/

locates the word hello on the current line and replaces it with goodbye. This can be done for every occurrence of the word on the line by appending g (for global) to the command:

:s/room 10/room 12/g

replaces each occurrence of room 10 on the line with room 12.

A range of lines to be affected by the ex command can be given:

:1,20s/terminal/Macintosh/g

replaces all occurrences of terminal with Macintosh in lines 1 to 20 of the file;

:1,$s/terminal/Macintosh/g

would do the substitution throughout the file.

Other useful ex commands are:

:r myfile

which reads file myfile and appends its contents after the current line, and

:w myfile

which writes the edit buffer to myfile. You can write the whole buffer to a file or you can specify a range of lines:

:1,.w startfile

writes all lines from the start of the file to the current line to file startfile (. means the current line).

The vi command Q will switch the editor to ex mode for all subsequent commands. The ex command :vi switches to screen mode. (Note that when you are in permanent ex mode, a colon : prompt is issued when ex can accept a command, so you shouldn't type it yourself).

In ex you cannot move around using the arrow keys but you can make any line the current line by giving its number or by referring to the line as an offset to the current line or by locating the line by context.

:10			# move to line 10
:+7			# move on 7 lines
:-3 			# move back 3 lines
:/string 		# move to the line containing string

A line may be marked with the :k command:

:ka

means mark the current line and call it a. You can later refer to the line as 'a:

:'a,.w save

writes lines from a to the current line to file save.

Finishing Up

The changes you make on the screen are kept in the internal memory of vi. This is called the edit buffer. The changes are not reflected in the file on UNIX until they are written to it. The command:

:w

writes the contents of the edit buffer to the file being edited. The command:

:q

then quits the editor and returns to the Shell.

You can do both these things in one step from vi by typing the characters

ZZ

Alternatively, from ex you can give the command:

:x

To quit without saving any changes use:

:q!

Other Commands

Vi/ex is a very powerful editor. This document should have given you enough information to perform simple edits.

For a fuller list of commands, see the Vi/Ex Quick Reference Note.