Unix vi Text Editor: Part One

The vi editor is a screen-oriented editor. If you are accustomed to VMS or a word-processor, there are a few features you will need to get used to. There are three modes, Command, Insert and ex. Arrow keys do not work to move the cursor in Insert mode. You must be in insert mode to type text that goes into the file; when you enter vi you are not in insert mode! There is nothing to tell you which mode you are in. If you type a command and the letter is inserted in your text, or if you type a letter and get beeped at (or worse, half your file disappears), you were in the wrong mode. You cannot move the cursor past the last character in the line and add on by using the insert command, you must use append instead. Ending a line with a space may cause problems with some commands. Adding material to lines already in the text may confuse the editor so that the cursor position you see and the position it acts on are no longer the same.

A skeleton vi session

% vi filename start the vi editor
i switch to insert mode [this i will not show]
[type in your text at this point ]
<ESC>: x or leave vi, saving the file
<ESC>: q! leave vi, do not save the changes

Starting vi

% vi filename start vi with cursor at first line
% vi + filename start vi with cursor at end of the text
% vi +25 filename start vi with cursor at line 25
% vi -r filename start vi and retrieve last saved version of the file

Entering command-mode and ex-mode commands

The <ESC> key switches you from insert mode to command mode. Commands which affect the entire file or parts of the file specified bv line numbers or marks, such as write or search commands, require that you type a colon (from command mode) to go to the command line with the : prompt before you enter the command; this is ex mode. You can see the command and change it until you press <RETURN>. Commands which affect the characters in the file at the cursor position let you enter the command itself with no colon, and the command does not echo on the screen. The command action happens immediately, no <RETURN> is necessary. The cursor-moving commands are letters typed in command mode.

Entering text

The following commands will all take you from command mode to insert mode.

i insert text before the cursor
I insert text at the beginning of the line
a append; insert text followmg the cursor
A append; insert text at the end of the line
o open a new line below the current line
O open a new line above the current line
r replace the current character with a new character
R replace characters with new ones until <ESC> is pressed

Moving the cursor

j move down one character
k move up one character
l move right one character
h move left one character

Joining two lines into one

J join two lines; deleting the carriage return will not join lines

Moving by larger chunks

CTRL-D scroll down (forward) half a screen
CTRL-U scroll up (backward) half a screen
CTRL-B scroll backward almost a whole screen
CTRL-F scroll forward almost a whole screen
H home position (top left corner)
M middle line
L lower line
150 G go to line 150
G go to end of text (= G with no line number)

Deleting text

x delete current character
5x delete 5 characters, beginning at current one
dd delete the current line
6dd delete 6 lines
d delete this line and the following one (look out!)
dspec delete some characters; spec may be a number, w (= to end of word), b (= to beginning of word), etc.

Undo

U undo last command


Previous Page
Index
Next Page