Labels

02/02/2011

Basic vi Commands in Linux

What is vi?

The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.]

The UNIX vi editor is a full screen editor and has two modes of operation:

Command mode commands which cause action to be taken on the file, and

Insert mode in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the (Escape) key turns off the Insert mode.

While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic.

NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.

To Get Into and Out Of vi

To Start vi

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

*

vi filename

edit filename starting at line 1

vi -r filename

recover filename that was being edited when system crashed

To Exit vi

Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.

Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the (or ) key.

*

:x

quit vi, writing out modified file to file named in original invocation

:wq

quit vi, writing out modified file to file named in original invocation

:q

quit (or exit) vi

*

:q!

quit vi even though latest changes have not been saved for this vi call

Moving the Cursor

Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.

If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.

In the table below, the symbol ^ before a letter means that the key should be held down while the letter key is pressed.

*

jor [or down-arrow]

move cursor down one line

*

k [or up-arrow]

move cursor up one line

*

hor [or left-arrow]

move cursor left one character

*

lor [or right-arrow]

move cursor right one character

*

0 (zero)

move cursor to start of current line (the one with the cursor)

*

$

move cursor to end of current line

w

move cursor to beginning of next word

b

move cursor back to beginning of preceding word

:0or 1G

move cursor to first line in file

:nor nG

move cursor to line n

:$or G

move cursor to last line in file

Screen Manipulation

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.

^f

move forward one screen

^b

move backward one screen

^d

move down (forward) one half screen

^u

move up (back) one half screen

^l

redraws the screen

^r

redraws the screen, removing deleted lines

Adding, Changing, and Deleting Text

Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.

Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.

*

u

UNDO WHATEVER YOU JUST DID; a simple toggle

The main purpose of an editor is to create, add, or modify text for a file.

Inserting or Adding Text

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

*

i

insert text before cursor, until hit

I

insert text at beginning of current line, until hit

*

a

append text after cursor, until hit

A

append text to end of current line, until hit

*

o

open and put text in a new line below current line, until hit

*

O

open and put text in a new line above current line, until hit

Changing Text

The following commands allow you to modify text.

*

r

replace single character under cursor (no needed)

R

replace characters, starting with current cursor position, until hit

cw

change the current word with new text, starting with the character under cursor, until hit