Using VI Text Editor



Before getting started, we suggest you Learn Linux Basics and follow these precautions.

Updated: 2019-03-10
Created: 2010-01-14

Text editors are programs used to create or edit files. One of the most popular editors on Linux/Unix systems (also available for other platforms) is vi. Though it does take some time getting use to, this easy to follow HowTo will walk you through the essentials of using vi.

Starting the vi text editor

The command to start the vi editor is simply vi, followed by the filename. For example to edit a file named linuxlookup, you would type:

vi linuxlookup

New files will be empty, where as existing files will be loaded into the text editor. When creating a new file you will see a screen filled with tilde characters (~) down the left side of the screen, these represent blank lines beyond the end of file. At the bottom of your screen, the filename will be shown, along with a line and character count. These come in handy for referencing.

Modes of Operation

Vi has two modes of operation, "command mode" and "insert-text mode". You're always in command mode when vi first starts. This command mode allows you to issue a desired action. While insert-text mode simply enters anything you type into the file. Through-out this document we're going to provided lists of frequently used commands, be sure to pay attention to case sensitivity as it does matter and can have different results.

Keystroke Description
Esc Hit the "Esc" (Escape) key at any point in time to enter command mode. If you were already in the command mode when you hit "Esc", don't worry. It might beep, but you will still be in the command mode.
Note: When in insert-text mode, you should see -- INSERT -- at the bottom of the window.

Properly exiting vi

In order to exit vi, you must first be in command mode. Type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, you may force quit command without saving. This allows you to view a file without actually saving any changes.

Keystroke Description
:q Quits the session.
:q! Forces a quit the session and does not save changes.
:w Writes (saves) changes to current file.
:wq Writes changes to current file and then quits.
:wq! Forces a write to current file and then quits.
Note: Files are not saved unless you issue the command to do so, thus closing the terminal to exit will result in the lost of any changes made to the file.

Inserting

Keystroke Description
i Inserts text to the left of the cursor.
I Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line.
a Begins inserting after the character (append) on which the cursor is positioned.
A Begins inserting at the end of the current line, no matter where the cursor is positioned on that line.
o Begins inserting text on a new, empty line, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
O Begins inserting text on a new, empty line, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.
r Replace single character under cursor position.
R Replace all characters, starting under cursor position, until Esc is pressed.

Deleting, Copying and Pasting

Keystroke Description
D Delete to end of the line.
dd Delete line at cursor position.
x Delete character right of the cursor position.
X Delete the character before the cursor.
d$ Delete to the end of the line, including the current character.
ndw Delete to the beginning of the line, excluding the current character. (n is a number)
ndW Delete a word(s), stopping at punctuation.
nde Delete to the end of next word.
ndd Delete a line(s).
y Yank (copy) the text at cursor position.
yy Yank (copy) the line at cursor position.
p Paste after the current cursor position.
P Paste before the current cursor position.

Navigating

Keystroke Description
h Move cursor to the left one character.
l Move cursor to the right one character.
j Move cursor down one line.
k Move cursor up one line.
^ Move cursor to the beginning of the line.
$ Move cursor to the end of the current line.
G Move cursor to the last line of your file.
1G Move cursor to the first line of your file.
w Move cursor forward to the next word, stopping at punctuation.
W Move cursor forward to the next word, ignoring punctuation.
e Move cursor forward to the end of the word, stopping at punctuation.
E Move cursor forward to the end of the word, ignoring punctuation.
:n n is the line number you'd like to jump to.
:set number Show line numbers down the left side of the window.
Note: The arrow keys can be used for basic navigation.

Search and Replace

Keystroke Description
f Search the current line for the character specified after the 'f' command. If found, move the cursor to the position.
F Search the current line backwards for the character specified after the 'F' command. If found, move the cursor to the position.
; Repeat the last f or F command (see above).
, Repeat the last f, F, t or T command in the reverse direction.
t Search the current line for the character specified after the 't' command, and move to the column before the character if found.
T Search the current line backward for the character specified after the 't' command, and move to the column before the character if found.
/ Search the file downwards for the string specified after the /.
? Search the file upwards for the string specified after the ?.
n Repeat last search given by '/' or '?'.
:s/old/new This will find the first occurrence of "old" and replace it with "new".
:s/old/new/g This will find globally (all) occurrences of "old" on the current line and replace them with "new".
%s/old/new/g This will find every occurrence of "old" in the file and replace them with "new".
%s/old/new/gc Same as above but asks for confirmation before changing.