Stuck in the Vi Editor?

Nazlıcan Yakar
Orion Innovation techClub
4 min readApr 25, 2023

Vi is a fast, lightweight, and efficient text editor for CLI that only requires keyboard inputs to operate. It is coded by Bill Joy in 1970 for UNIX-based operating systems and stands for “Visual Editor”. But who exactly uses Vi and how?

UNIX-based OS like CentOS/RHEL is heavily employed in the industry. Unfortunately, CentOS, for example, does not have a GUI. One can only interact with the computer via the CLI. To edit the configurations, and scripts, and create text files, developers need Vi.

A base level of knowledge of Linux CLI will be enough to use Vi without any problems. Anywhere in the Linux file system that your user has a privilege, you can type the following command to ignite Vi to edit a file named test.txt in the current directory and create the file if it previously did not exist.

vi test.txt

This command will leave you with the following CLI:

Vi editor screen for test.txt file at initialization.

For those who are familiar with the Computer Science term state machine, Vi is pretty easy to understand. Vi has 3 different states and can go back and forth to edit, save, and exit from a file. In the beginning, it is in the command mode, waiting for an input command to change its state. The state change inputs are received from different keys, which you can follow with the state diagram below.

Vi State Diagram

To write into the file, you should press the “i” or “insert” key on your keyboard. Now, Vi is in insert mode. In this state, whatever you type will be written into the file.

Insert mode

After you make the changes and want out, there are two options:

  1. You want to save the changes.
    First, you should exit the insert mode and go to command line mode. To do so, press the “Esc” key. Then type “:wq” to save the changes, where w stands for write and q stands for quit.
  2. You want to discard them.
    Again, press the ESC key and then type either “:q!” or “:q”.
:wq     # save the changes
:q!, :q # discard the changes
Save changes — this is in Command mode.

Congratulations! You created your first text file with Vi editor without getting stuck. Creating and modifying the text files, this is the basis. However, there is more to Vi. For example, you can go to a specific line in the document by typing “:” and the line number, search a word by typing “:/” before the word, and delete a line completely by typing “dd” all when your Vi is in command mode.

Further advanced operations are also possible. The following list[1] is a glimpse of the powerful commands you can use in Vi:

  • $ vi <filename> — Open or edit a file.
  • i — Switch to Insert mode.
  • Esc — Switch to Command mode.
  • :w — Save and continue editing.
  • :wq or ZZ — Save and quit/exit vi.
  • :q! — Quit vi and do not save changes.
  • yy — Yank (copy) a line of text.
  • p — Paste a line of yanked text below the current line.
  • o — Open a new line under the current line.
  • O — Open a new line above the current line.
  • A — Append to the end of the line.
  • a — Append after the cursor’s current position.
  • I — Insert text at the beginning of the current line.
  • b — Go to the beginning of the word.
  • e — Go to the end of the word.
  • x — Delete a single character.
  • dd — Delete an entire line.
  • Xdd — Delete X number of lines.
  • Xyy — Yank X number of lines.
  • G — Go to the last line in a file.
  • XG — Go to line X in a file.
  • gg — Go to the first line in a file.
  • :num — Display the current line’s line number.
  • h — Move left one character.
  • j — Move down one line.
  • k — Move up one line.
  • l — Move right to one character.

I believe you have become somewhat familiar with the use of Vi now. If you want to learn more about Vi, plenty of online sources embody the cheats, tips, and tricks for developers to use it effectively.

You will never get stuck in Vi again!

References:
[1] Khess. (2023). An introduction to the vi editor. Enable Sysadmin. https://www.redhat.com/sysadmin/introduction-vi-editor

--

--