How does the VI editor work?

How does the VI editor work?

December 13, 2022 0 Door Bjorn Meijer

The VI editor is a handy tool and comes standard in almost all Linux distributions. It is also standard in the shell of Home Assistant. If you don't know exactly how the editor works, it can be difficult to get started. For that, this small, but simple manual about the VI editor.

The VI editor is nowadays available in more extensive versions such as VIM, Elvis, Nvi, Nano and Vile.

The VI editor has several modes in which you can work:

  • command mode;
  • editor mode.

Open file in VI editor

To open a file in the VI editor use the following command:

vi filename

If the file exists, the file will be opened in the VI editor. If the file does not exist, the VI editor will create the file.

Create file with VI editor
Create file with VI editor

Editor mode

In editor mode it is possible to insert, edit or delete text. The editor mode can be activated by the i pressing on the keyboard. Each key on the keyboard is considered an input and appears on the screen. If you want to return to the commandmode, press the Esc key in.

VI editor mode
VI editor mode

command mode

By default, the VI editor opens in command mode. As the name suggests, the editor only responds to commands. The command mode also saves the changes made to the document. It should be noted that the commands are case sensitive.

In VI editor you cannot move the cursor with the mouse like in Windows. There are a number of key commands for this. On some UNIX platforms you can also use the arrow keys. However, this is not recommended because it can sometimes cause unwanted effects. To move the cursor within a file you must be in command mode. To move the cursor you can use the following keys:

  • j – move cursor down
  • k – move cursor up
  • h – move cursor one character to the left
  • l – move cursor one character to the right

In addition to moving the cursor, we have a number of commands that can be used to make textual adjustments:

  • i – add text where the cursor is located
  • a – add text after the cursor
  • A – add text at the end of the line
  • u – undo last change
  • U – undo all changes to the entire rule
  • o – create new rule
  • dd – delete line
  • 3dd – delete three lines
  • D – delete the contents of the line after the cursor
  • C – delete the contents of a line after the cursor and insert new text. Press the ESC key to end the insertion.
  • dw – delete word
  • 4dw – remove four words
  • cw – change word
  • x – delete character at the cursor
  • r – replace character
  • R – overwrite characters from the cursor
  • s – replace a character under the cursor and keep inserting
  • S – replace the whole line and start inserting at the beginning of the line
  • ~ – change the capitalization of an individual character
Remove four words with VI editor
Remove four words with VI editor

Close and save file

You also need to be in command mode to close and save a file. The following key combinations can be used to close and save a file.

  • Shift+zz – save and exit file
  • :w – Save file and do not exit
  • :q! – Exit VI editor and do not save changes
  • :wq – Save file and exit VI editor
Save file and close
Save file and close