Chapter #2: Mastering Vi/Vim Command-Line Text Editor
In this chapter, we show you how to start using Vi or Vim in any Linux system, covering installation, command modes, editing, saving files, and more.

Vi was the first full-screen text editor written for Unix. Although it was intended to be small and simple, it can be a bit challenging for people used exclusively to GUI text editors, such as NotePad++, or gedit, to name a few examples.
To use Vi, we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about the its powerful text-editing procedures.
Note: Vi is also available in minimal environments, such as rescue modes or containerized systems, where no other editor may be installed.
Please note that most modern Linux distributions ship with a variant of vi
known as vim
(โVi improvedโ), which supports more features than the original vi
does. For that reason, throughout this tutorial we will use vi
and vim
interchangeably.
If your distribution does not have vim installed, you can install it as follows:
sudo apt install vim #Ubuntu and derivatives
sudo yum install vim #Red Hat-based distributions
Why Should I Want to Learn Vi?
There are at least 2 good reasons to learn vi:
- vi is always available (no matter what distribution youโre using) since it is required by POSIX.
- vi does not consume a considerable amount of system resources and allows us to perform any imaginable tasks without lifting our fingers from the keyboard.
Extra Tip: vi is also extremely fast and lightweight, making it ideal for quick edits over SSH or in low-bandwidth situations.
In addition, vi has a very extensive built-in manual, which can be launched using the :help
command right after the program is started. This built-in manual contains more information than vi/mโs man page.
Launching Vi Editor
To launch vi, type vi
in your command prompt, then press i
to enter Insert mode, and you can start typing.
Another way to launch vi/m is:
vi filename
which will open a new buffer (more on buffers later) named filename
, which you can later save to disk.
Tip: You can also open multiple files at once by separating filenames with spaces: vi file1.txt file2.txt
Understanding Vi Modes
Vi operates in different modes, each serving a specific purpose. To use it effectively, it's important to know how to switch between them and what each mode does.
Command Mode
vi allows the user to navigate around the file and enter vi commands, which are brief, case-sensitive combinations of one or more letters. Almost all of them can be prefixed with a number to repeat the command that number of times.
For example, yy
(or Y
) copies the entire current line, whereas 3yy
(or 3Y
) copies the entire current line along with the two next lines (3 lines in total). We can always enter command mode (regardless of the mode weโre working on) by pressing the Esc
key.
The fact that in command mode the keyboard keys are interpreted as commands instead of text tends to be confusing to beginners.
Ex Mode
We can manipulate files (including saving a current file and running outside programs). To enter this mode, we must type a colon (:
) from command mode, directly followed by the name of the ex-mode command that needs to be used. After that, vi returns automatically to command mode.
Insert Mode
The letter i
is commonly used to enter this mode. We simply enter text. Most keystrokes result in text appearing on the screen (one important exception is the Esc
key, which exits insert mode and returns to command mode).
v
(character-wise), V
(line-wise), or Ctrl-v
(block-wise) in command mode.Hereโs how you can use visual mode practically:
- Press
v
, then move the cursor (j/k/h/l
) to select text. - Press
d
to delete the selection,y
to yank (copy), or>
to indent.
Examples:
vjjd # Delete current line and two lines below
V2j> # Indent three lines
Ctrl-vjjx # Delete a block selection
Vi Commands
The following table shows a list of commonly used vi commands. File edition commands can be enforced by appending the exclamation sign to the command (for example, :q!
enforces quitting without saving).