New Document
How to Use the vi Editor*

The vi editor is available on almost all Unix systems. vi can be used from any type of terminal because it does not depend on arrow keys and function keys--it uses the standard alphabetic keys for commands.

vi (pronounced "vee-eye") is short for "vi"sual editor. It displays a window into the file being edited that shows 24 lines of text. vi is a text editor, not a "what you see is what you get" word processor. vi lets you add, change, and delete text, but does not provide such formatting capabilities as centering lines or indenting paragraphs..

This help note explains the basics of vi:

  • opening and closing a file.

  • moving around in a file.

  • elementary editing.

Starting vi:

You may use vi to open an already existing file by typing:

  vi filename

where "filename" is the name of the existing file. If the file is not in your current directory, you must use the full pathname. Or you may create a new file by typing:

  vi newname

where "newname" is the name you wish to give the new file. To open a new file called "testvi," enter.

  vi testvi

On-screen, you will see blank lines, each with a tilde (~) at the left, and a line at the bottom giving the name and status of the new file.

  "testvi" [New file]
vi Modes

vi has two modes:

  • command mode
  • insert mode

In command mode, the letters of the keyboard perform editing functions (like moving the cursor, deleting text, etc.). To enter command mode, press the escape key.

In insert mode, the letters you type form words and sentences. Unlike many word processors, vi starts up in command mode.

Entering Text

In order to begin entering text in this empty file, you must change from command mode to insert mode. To do this, type 'i'

Nothing appears to change, but you are now in insert mode and can begin typing text. In general, vi's commands do not display on the screen and do not require the Return key to be pressed.

Type a few short lines and press at the end of each line. If you type a long line, you will notice the vi does not word wrap, it merely breaks the line unceremoniously at the edge of the screen.

If you make a mistake, pressing or may remove the error, depending on your terminal type.

Moving the Cursor

To move the cursor to another position, you must be in command mode. If you have just finished typing text, you are still in insert mode. Go back to command mode by pressing . If you are not sure which mode you are in, press once or twice until you hear a beep. When you hear the beep, you are in command mode.

The cursor is controlled with four keys: h, j, k, l.

     Key        Cursor Movement
     ---        ---------------

     h        left one space
     j        down one line
     k        up one line
     l        right one space

When you have gone as far as possible in one direction, the cursor stops moving and you hear a beep. For example, you cannot use l to move right and wrap around to the next line, you must use j to move down a line. See the section entitled "Moving Around in a File" for ways to move more quickly through a file.

Basic Editing

Editing commands require that you be command mode. Many of the editing commands have a different function depending on whether they are typed as upper- or lowercase. Often, editing commands can be preceded by a number to indicate a repetition of the command.

Deleting Characters

To delete a character from a file, move the cursor until it is on the incorrect letter, then type x

The character under the cursor disappears. To remove four characters (the one under the cursor and the next three) type 4x

To delete the character before the cursor, type X (uppercase)

Deleting Words

To delete a word, move the cursor to the first letter of the word, and type dw

This command deletes the word and the space following it. To delete three words type 3dw

Deleting Lines

To delete a whole line, type dd

The cursor does not have to be at the beginning of the line. Typing dd deletes the entire line containing the cursor and places the cursor at the start of the next line. To delete two lines, type 2dd

To delete from the cursor position to the end of the line, type D (uppercase)

commands in the editor
CommandDescription
0 or |Positions cursor at beginning of line.
$Positions cursor at end of line.
wPositions cursor to the next word.
bPositions cursor to previous word.
(Positions cursor to beginning of current sentence.
)Positions cursor to beginning of next sentence.
EMove to the end of Blank delimited word
{Move a paragraph back
}Move a paragraph forward
[[Move a section back
]]Move a section forward
n|Moves to the column n in the current line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
nHMoves to nth line from the top of the screen
MMove to middle of screen
LMove to botton of screen
nLMoves to nth line from the bottom of the screen
:xColon followed by a number would position the cursor on line number represented by x

Control Commands:

There are following useful command which you can use along with Control Key:

CommandDescription
CTRL+dMove forward 1/2 screen
CTRL+dMove forward 1/2 screen
CTRL+fMove forward one full screen
CTRL+uMove backward 1/2 screen
CTRL+bMove backward one full screen
CTRL+eMoves screen up one line
CTRL+yMoves screen down one line
CTRL+uMoves screen up 1/2 page
CTRL+dMoves screen down 1/2 page
CTRL+bMoves screen up one page
CTRL+fMoves screen down one page
CTRL+IRedraws screen

Editing Files:

To edit the file, you need to be in the insert mode. There are many ways to enter insert mode from the command mode:

CommandDescription
iInserts text before current cursor location.
IInserts text at beginning of current line.
aInserts text after current cursor location.
AInserts text at end of current line.
oCreates a new line for text entry below cursor location.
OCreates a new line for text entry above cursor location.
Deleting Characters:

Here is the list of important commands which can be used to delete characters and lines in an opened file:

CommandDescription
xDeletes the character under the cursor location.
XDeletes the character before the cursor location.
dwDeletes from the current cursor location to the next word.
d^Deletes from current cursor position to the beginning of the line.
d$Deletes from current cursor position to the end of the line.
DDeletes from the cursor position to the end of the current line.
ddDeletes the line the cursor is on.

As mentioned above, most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two character under the cursor location and 2dd deletes two lines the cursor is on.

I would highly recommend to exercise all the above commands properly before proceeding further.

Change Commands:

You also have the capability to change characters, words, or lines in vi without deleting them. Here are the relevant commands:

CommandDescription
ccRemoves contents of the line, leaving you in insert mode.
cwChanges the word the cursor is on from the cursor to the lowercase w end of the word.
rReplaces the character under the cursor. vi returns to command mode after the replacement is entered.
ROverwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
sReplaces the current character with the character you type. Afterward, you are left in insert mode.
SDeletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode.
Copy and Past Commands:

You can copy lines or words from one place and then you can past them at another place using following commands:

CommandDescription
yyCopies the current line.
ywCopies the current word from the character the lowercase w cursor is on until the end of the word.
pPuts the copied text after the cursor.
PPuts the yanked text before the cursor.

Advanced Commands:

There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi:

CommandDescription
JJoin the current line with the next one. A count joins that many lines.
<<Shifts the current line to the left by one shift width.
>>Shifts the current line to the right by one shift width.
~Switch the case of the character under the cursor.
^GPress CNTRL and G keys at the same time to show the current filename and the status.
URestore the current line to the state it was in before the cursor entered the line.
uUndo the last change to the file. Typing 'u' again will re-do the change.
JJoin the current line with the next one. A count joins that many lines.
:fDisplays current position in the file in % and file name, total number of file.
:f filenameRenames current file to filename.
:w filenameWrite to file filename.
:e filenameOpens another file with filename.
:cd dirnameChanges current working directory to dirname.
:e #Use to toggle between two opened files.
:nIn case you open multiple files using vi, use :n to go to next file in the series.
:pIn case you open multiple files using vi, use :p to go to previous file in the series.
:NIn case you open multiple files using vi, use :N to go to previous file in the series.
:r fileReads file and inserts it after current line
:nr fileReads file and inserts it after line n.

Word and Character Searching:

The vi editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for.

These two commands differ only in the direction where the search takes place:

  • The / command searches forwards (downwards) in the file.

  • The ? command searches backwards (upwards) in the file.

The n and N commands repeat the previous search command in the same or opposite direction, respectively. Some characters have special meanings while using in search command and preceded by a backslash (\) to be included as part of the search expression.

CharacterDescription
^Search at the beginning of the line. (Use at the beginning of a search expression.)
.Matches a single character.
*Matches zero or more of the previous character.
$End of the line (Use at the end of the search expression.)
[Starts a set of matching, or non-matching expressions.
<Put in an expression escaped with the backslash to find the ending or beginning of a word.
>See the '<' character description above.

The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character.

The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.

Set Commands:

You can change the look and feel of your vi screen using the following :set commands. To use these commands you have to come in command mode then type :set followed by any of the following options:

CommandDescription
:set icIgnores case when searching
:set aiSets autoindent
:set noaiTo unset autoindent.
:set nuDisplays lines with line numbers on the left side.
:set swSets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4
:set wsIf wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
:set wmIf this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm=2
:set roChanges file type to "read only"
:set termPrints terminal type
:set bfDiscards control characters from input

Running Commands:

The vi has the capability to run commands from within the editor. To run a command, you only need to go into command mode and type :! command.

For example, if you want to check whether a file exists before you try to save your file to that filename, you can type :! ls and you will see the output of ls on the screen.

When you press any key (or the command's escape sequence), you are returned to your vi session.

Replacing Text:

The substitution command (:s/) enables you to quickly replace words or groups of words within your files. Here is the simple syntax:

:s/search/replace/g

The g stands for globally. The result of this command is that all occurrences on the cursor's line are changed.

IMPORTANT:

Here are the key points to your success with vi:

  • You must be in command mode to use commands. (Press Esc twice at any time to ensure that you are in command mode.)

  • You must be careful to use the proper case (capitalization) for all commands.

  • You must be in insert mode to enter text.

Previous                                                                                                                                                       Next

Back to Top