What is vi?

Author: 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:

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.

  1. Press i to enter insert mode.
  2. Type a sentence, such as This is my first note.
  3. Press Esc to stop entering text.
  4. Type :wq and 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.

CommandAction
h, j, k, lMove left, down, up and right, respectively
0Go to the beginning of the line
$Go to the end of the line
xDelete the character under the cursor
ddDelete the current line
uUndo the last change
/search textSearch for text towards the end of the file
nRepeat the search in the same direction
:25Go 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?

Read also

Learn how PHP-FPM handles PHP requests, how it differs from a web server, and how to tune processes for traffic and hosting resources.
Learn how BIND, open-source DNS server software, handles domains, how its two roles differ and when running it yourself makes sense.
Learn what Poland’s REGON register and identification number mean, how they differ from NIP and KRS, and how to check a business’s details.