By Emmett Dulaney

If you have the opportunity to dabble with ed in Linux, you’ll find that vi is a dream come true, even though it’s still a command-line editor. In Linux. the vi editor is a full-screen text editor, so you can view several lines at the same time. Most Unix systems, including Linux, come with vi. Therefore, if you know the basic features of vi, you can edit text files on almost any Unix-based system.

When vi edits a file, it reads the file into a buffer — a block of memory — so you can change the text in the buffer. The vi editor also uses temporary files during editing, but the original file isn’t altered until you save the changes.

To start the editor in a Linux system, type vi followed by the name of the file you want to edit, like this:

vi /etc/fstab

The vi editor loads the file into memory, displays the first few lines in a text screen, and positions the cursor on the first line.

vi editor Linux
You can edit files with the vi full-screen text editor.

The last line shows the pathname of the file as well as the number of lines (2) and the number of characters (59) in the file. In this case, the text [readonly] appears after the filename because /etc/fstab file is opened while the user is logged in as a normal user (which means that this person doesn’t have permission to modify the file). Later, the last line in the vi display functions as a command entry area. The rest of the lines display the file. If the file contains fewer lines than the screen, vi displays the empty lines with a tilde (~) in the first column.

The current line is marked by the cursor, which appears as a small black rectangle. The cursor appears on top of a character.

When using vi, you work in one of three modes in Linux:

  • Visual command mode: This mode is the default. In this mode, anything you type is interpreted as a command that applies to the line containing the cursor. The vi commands are similar to the ed commands.
  • Colon command mode: You use this mode for reading or writing files, setting vi options, and quitting vi. All colon commands start with a colon (:). When you type the colon, vi positions the cursor on the last line and waits for you to type a command. The command takes effect when you press Enter.
  • Text-input mode: This mode is for typing text. You can enter text-input mode with the command a (insert after cursor), A (append at end of line), o (open a line below the current one), O (open a line above the current one), or i (insert after cursor). After entering lines of text, you have to press Esc to leave text-input mode and reenter visual command mode.

One problem with all these modes is that you can’t easily tell the current mode that vi is in. You may begin typing only to realize that vi isn’t in text-input mode, which can be frustrating.

If you want to make sure that vi is in command mode, press Esc a few times. (Pressing Esc more than once doesn’t hurt.)

To view online help in vi, type :help while in colon command mode. When you’re finished with help, type :q to exit the Help screen and return to the file you’re editing.

The vi editor initially positions the cursor on the first character of the first line, and one of the handiest things you can know is how to move the cursor around. To get a bit of practice, try the following command.

Cursor Movement Commands in vi
Key Moves the Cursor
One line down
One line up
One character to the left
One character to the right
W One word forward
B One word backward
Ctrl+D Half a screen down
Ctrl+U Half a screen up

You can go to a specific line number at any time by using the handy colon command. To go to line 6, for example, type the following and then press Enter:


When you type the colon, vi displays the colon on the last line of the screen. From then on, vi uses any text you type as a command. You have to press Enter to submit the command to vi. In colon command mode, vi accepts all commands that the ed editor accepts, and then some.

To search for a string, first type a slash (/). The vi editor displays the slash on the last line of the screen. Type the search string and then press Enter. The vi editor locates the string and positions the cursor at the beginning of that string. To locate the string cdrom in the file /etc/fstab, type


To delete the line that contains the cursor, type dd. The vi editor deletes that line of text and makes the next line the current one.

To begin entering text in front of the cursor, type i. The vi editor switches to text-input mode. Now you can enter text. When you finish entering text, press Esc to return to visual command mode.

After you finish editing the file, you can save the changes in the file with the :w command. To quit the editor without saving any changes, use the :q! command. If you want to save the changes and exit, you can use the :wq command to perform both steps at the same time. The vi editor saves the changes in the file and exits. You can also save the changes and exit the editor by pressing Shift+ZZ (that is, hold Shift down and press Z twice).

vi accepts a large number of commands in addition to the commands just mentioned. The table below lists some commonly used vi commands, organized by task.

Common vi Commands
Command Does the Following
Insert text
a Inserts text after the cursor
A Inserts text at the end of the current line
I Inserts text at the beginning of the current line
i Inserts text before the cursor
Delete text
D Deletes up to the end of the current line
dd Deletes the current line
dG Deletes from the current line to the end of the file
dw Deletes the current word where the cursor presently resides
x Deletes the character on which the cursor rests
Change text
C Changes up to the end of the current line
cc Changes the current line
J Joins the current line with the next one
rx Replaces the character under the cursor with x (where x is any character)
Move cursor
h or ← Moves one character to the left
j or ↓ Moves one line down
k or ↑ Moves one line up
L Moves to the end of the screen
l or → Moves one character to the right
w Moves to the beginning of the following word
b Moves to the beginning of the previous word
Scroll text
Ctrl+D Scrolls forward by half a screen
Ctrl+U Scrolls backward by half a screen
Refresh screen
Ctrl+L Redraws the screen
Cut and paste text
yy Yanks (copies) current line to an unnamed buffer
P Puts the yanked line above the current line
p Puts the yanked line below the current line
Colon commands
:!command Executes a shell command
:q Quits the editor
:q! Quits without saving changes
:r filename Reads the file and inserts it after the current line
:w filename Writes a buffer to the file
:wq Saves changes and exits
Search text
/string Searches forward for a string
?string Searches backward for a string
u Undoes the last command
Esc Ends input mode and enters visual command mode
U Undoes recent changes to the current line