What is vi?
Author: Domeny.ForSale editorial team Published
In brief: vi is a terminal-based text editor used primarily on Unix and Linux systems. It is controlled through keyboard commands, and what the keys do depends on the selected mode. To start typing, press i; to save the file and quit, press Esc, type :wq and press Enter. To quit without saving changes, press Esc, then type :q! and press Enter.
What is vi used for?
vi is used to edit plain text files: program configurations, scripts, source code and notes. It is not a document editor like Word — it is not intended for setting fonts, arranging page layouts or embedding illustrations. It works on the contents of a file, without additional document formatting.
It is especially useful when working on a server over SSH, an encrypted connection to another computer. When you only have access to a terminal, you can modify a file without launching a graphical desktop or downloading it to your computer.
The vi editor or a compatible replacement is available in many Unix environments. However, you should not assume that you will find it in every installation: a minimal system or container image may not include any editor.
Why does vi have different modes?
In a typical editor, pressing a letter immediately adds it to the text. In vi, after opening a file, you are usually in normal mode, also called command mode. Letters are then used to move around the file, delete text or perform other operations.
To get started, you only need to distinguish between three ways of working:
- Normal mode: you issue commands; for example,
dddeletes the current line. PressEscto return to this mode. - Insert mode: you enter text. From normal mode, press
ito enter insert mode and begin inserting before the cursor. - Command line: after pressing
:in normal mode, you type a command, such as:wto save the file. Press Enter to run it.
This distinction explains a common surprise for beginners: they press letters, but instead of typing, they move the cursor or change existing text. If you do not know which mode you are in, press Esc. Only then choose your next action.
Editing your first file step by step
The safest approach is to practise on a new file rather than on the configuration of a running service. In the terminal, run:
vi note.txt
If the file exists, the editor will open its contents. If it does not exist, the editor will prepare a new document in memory; the file will only be created on disk once it has been successfully saved.
- Press
ito enter insert mode. - Type a sentence, such as
This is my first note. - Press
Escto stop entering text. - Type
:wqand press Enter.
After saving the file and closing the editor, you will return to the shell, the program that accepts commands in the terminal. You can run vi note.txt again to check the contents.
To save your work but continue editing, use :w. The command :q on its own closes the editor, provided there are no unsaved changes requiring a decision. The command :q! discards unsaved changes — it does not delete the existing file from disk.
Basic commands that make work easier
Use the following commands in normal mode. A colon or slash starts a command that you confirm by pressing Enter; the other shortcuts work without this confirmation.
| Command | Action |
|---|---|
h, j, k, l | Move left, down, up and right, respectively |
0 | Go to the beginning of the line |
$ | Go to the end of the line |
x | Delete the character under the cursor |
dd | Delete the current line |
u | Undo the last change |
/search text | Search for text towards the end of the file |
n | Repeat the search in the same direction |
:25 | Go to line number 25 |
For example, to find the timeout setting, press Esc, type /timeout and press Enter. The n key takes you to the next occurrence. This is more convenient than manually browsing a long configuration file.
Are vi and Vim the same program?
No. Vim, whose name stands for Vi IMproved, is a separate editor that builds on vi’s way of working. It offers additional features, such as extensive syntax highlighting, plugins and multi-level undo.
On some systems, the vi command actually launches Vim or another compatible editor. This means that the screen’s appearance and the available extensions may differ. The basic commands described here are part of their shared way of working, but not every Vim tutorial will apply in full to classic vi.
Before editing an important configuration file, make a copy of it. If saving fails with a permissions error, simply adding ! will not give you permission to access the file. Start by practising on your own note, and after changing a configuration, check it using the service’s validation tool, if one is available.
See also: What is GitLab?
