The vi editor
Contents
The vi
editor¶
This file:
Provides the basic instructions for using the
vi
editor.
Keywords: ASCII, vi
, command mode, insert mode, vim
Associated material:
The vi
editor is the basic ASCII text editor that comes with essentially all Linux/Unix systems. It works on the command line without GUI. It is very lightweight.
vi
differs from most editors (with or without GUI) in that it has two modes: 1) command mode and 2) editing or insert mode. As the names suggest, these modes serve for different purposes. In the command mode, one can enter commands such as copy
, paste
, save
, quit
and so on. Editing text is not possible in the command mode. For editing text, one has to enter the editing or insert mode. That mode is exclusively for editing text and for saving the file, one has to switch to command mode. Understanding the two modes is the key to using the vi
editor.
Why vi
¶
First, since it ships with all Linux/Unix systems, it is always there without any additional installations. Second, is also extremely lightweight. Those two factors are very important if something goes wrong with the systems and some of the system files need to be edited; in such case no GUI based editors (even if they are ASCII editors) may be impossible to use. Although switching between the modes may sound cumbersome, it is very easy to get used to that and vi
is a favorite of many.
Switching between the modes¶
When
vi
is started, it is always in the command mode.Commands are case sensitive
Switch from the command mode to the insert mode: press the key
i
.Switch from the insert mode to the command mode: press the
esc
key.
Starting vi
¶
To open a file or to create a new file, simply type
vi filename.txt
If filename.txt
exists it will be simply opened. If it doesn’t exist, it will be created.
vi
command summary¶
Below is a summary of some of the most common commands. Open a test file and try them.
COMMAND (the commands must be given in the command mode) |
FUNCTION |
NOTES |
---|---|---|
|
edit/create a file called |
On the command line terminal. Starts |
|
Switch to insert mode |
Shows the text |
|
Switch to command mode |
|
|
delete character to the right of the cursor |
|
|
delete 4 characters (to the right of the cursor) |
|
|
delete character to the left of the cursor |
|
|
delete word to the right of the cursor |
|
|
delete 2 words to the right of the cursor |
|
|
delete the current line |
|
|
delete 2 (current and the next) lines |
|
|
delete all characters from the cursor to end of the line |
|
|
insert an empty line below the cursor |
Switches to insert mode |
|
insert an empty line above the cursor |
Switches to insert mode |
|
replace the character under the cursor |
Switches to insert mode |
|
append text to the right of the cursor |
Switches to insert mode |
|
replace the word |
Switches to insert mode |
|
replace 3 words (to the right; leaves the rest the line after the two words intact) |
Switches to insert mode |
|
delete the text from cursor to end of line |
Switches to insert mode |
|
join the line below to the current line |
Switches to insert mode. The cursor can be anywhere on the line |
|
join the 3 following lines below to the current line |
Switches to insert mode. The cursor can be anywhere on the line |
|
undo |
Switches to insert mode |
|
save the file and quit |
Switches to insert mode |
|
save the file |
Switches to insert mode |
|
save the file with name |
Switches to insert mode |
|
read text into the current file from file named |
Switches to insert mode |
|
save the file and quit |
Switches to insert mode |
|
discard all changes since last save and quit |
Switches to insert mode |
|
move forward word-by-word |
|
|
move backward word-by-word |
|
|
move to the end of the current line |
|
|
move to the beginning of the current line |
|
|
go to the top line of the current screen |
|
|
go to the middle line of the current screen |
|
|
go to last line of the current screen |
|
|
go to the last line of the file |
|
|
go to first line of the file |
|
|
scroll forward one screen |
|
|
scroll backward one screen |
|
|
scroll down one-half screen |
|
|
scroll up one-half screen |
|
|
search for keyword in the current text |
To repeat the search forward press |
|
search for keyword in the current line |
To repeat the search forward press |
|
search for |
in command mode. |
|
search for all occurrences of |
in command mode. |
vi
cursor movements:¶
Instead of using the arrows, the cursors (in command mode) can be moved using the following keys:
COMMAND |
FUNCTION |
---|---|
|
move left one space |
|
move down one line |
|
move up one line |
|
move right one space |
vi
in readonly-mode¶
vi
can also be used in readonly-mode. The search and movement functions work as listed above, but editing and saving is not possible in the readonly mode. Here’s how:
vi -R filename.txt
vim
the basic vi
improved¶
vim
is an improved vi
and a very popular ASCII text editor - vi
and vim
are arguably the most popular text editor in Linux/Unix. vim
adds several features to the basic vim
and all of the above commands work also in vim
. It comes (just like the basic vi
) with most (if not all) current Linux/Unix systems as well as Mac.
vim
stands for Vi IMproved. It adds features to the basic vi
and is highly customizable. It also has many plugins including git
integration and a scripting language. vim
is available for essentially all operating systems including iOS and Android.