A Beginner's Guide: Vi Text Editor Explained.

In this article, we will explore the Visual Instrument (vi) text editor and cover some basic actions you can carry out.

Here are the things you will learn:

  • What vi text editor is.

  • How to use vi.

  • Commands in vi.

Prerequisites

  • Installation of Ubuntu.

  • Installation of a terminal (you can use a Windows terminal).

  • Average understanding of operating a terminal.

Note: I installed Ubuntu 20.04 LTS on my Windows system and then installed vi by executing the sudo apt install vi command. To follow a demonstration of the installation process, see this Demonstration Link.

Now that's addressed, let's talk about the vi text editor

What is vi?

Vi represents the original full-screen editor for Uniplexed information computing systems (UNIX). In 1976, Bill Joy wrote the editor as a graduate student at UC Berkeley. The text editor provides a user-friendly experience and functions consistently across different distributors and platforms. The editor can be found in all UNIX and UNIX-like systems. Examples are Linux, SunOS, Solaris, and NetBSD. e.t.c

The vi text editor features multiple modes:

  • Command Mode: Vi text editor starts in command mode by default. In this mode, the editor treats typed words as commands rather than text. You can perform actions on your file using commands.

Note: Commands in command mode are case-sensitive.

  • Insert Mode: Text can only be inserted into your file in this state. Pressing the Esc key will transition you back to command mode from the insert mode.

  • Command-line Mode: You initiate commands in this mode with a colon (:) symbol as a prefix. To access this mode, navigate from either the command or insert mode. This mode displays commands inputted at the bottom left corner.

To enter the default mode (command mode) when unsure about your current state, press the Esc key twice.

How to use vi

First, open up a terminal and create a file to interact with vi. Allow me to explain the process of creating a file named example in vi within the terminal of your choice:

  1. Open terminal

  2. Enter vi

  3. Input example

  4. Press Enter

The system generates a file if the name you used doesn't previously exist. Otherwise, the system opens the existing file instead of creating a new one.

Creating a new file called example is the process shown in the preceding screenshot. Pressing Enter then leads to the following snapshot, which is the command mode.

Command mode

You are in command mode by default. The snapshot below is empty because it's a new file. The system treats typed words in this mode as a command rather than text. Typing randomly in this mode does not display anything on your screen.

The terminal window displays the file name at the bottom left. Commands in command mode are case-sensitive. For example, p lets you paste after the current line you are at while P is for pasting before the current line you are at.

To enter text, click i on your keyboard to activate the insert mode.

Insert Mode

In insert mode, you can type anything. To proceed to the following line in this mode, press enter. The insert mode is indicated at the bottom left corner with INSERT in the following image. Once you are done with typing, press the Esc key to return to the command mode for inputting commands.

Command-line Mode

In command-line mode, you initiate commands with a colon (:) symbol as a prefix. To access this mode, navigate from either the command or insert mode. Switching to this mode from command mode is as simple as entering a colon (:). When switching from insert mode, press Esc, then type a colon (:). The system displays commands inputted at the bottom left corner.

The command :wq for saving your file and quitting vi is shown in the preceding image. The prefix (:) indicates it is a command-line command, as displayed in the bottom left.

Commands in Vi

In the vi text editor, commands are executed by individual characters, such as i,w, and so on, or by combining them like :wq. These commands enable you to perform a range of actions within vi. For example, save and exit a file.

Save and exit a file

You can save a file in vi in a few different ways. You can use the following commands :w, :wq, :q, :!q, and ZZ.

:w

You save files using :w. The file gets saved without providing feedback. The command appears at the bottom left corner as a command-line action with the prefix (:). An example is shown in the following image:

:wq

This is used to save and exit a file in command-line mode. This command saves the file and then exits vi. An example is shown in the following image:

You have saved the file and exited vi as indicated by the following feedback:

:q

Use the command :q, to quit without saving your file. The command only functions if you haven't made any changes unless you will receive an error message indicating that the file has been modified.

I received the following error message because I had modified the content with Isn't it?

:!q

The command :!q saves your file while discarding any changes you made to it. With this command, you can save the file in the preceding image. It discards the changes you made to the file and then saves it.

ZZ

The command ZZ saves and exits your file in command mode. Remember that commands in this mode are case-sensitive, so typing zz in lowercase does not have any effect. Typing ZZ in upper case has the intended effect of saving and exiting your file.

More commands in Vi

This list outlines the commands you can execute in command mode within vi. Remember, the vi editor treats commands in command mode as case-sensitive. Furthermore, the cursor within vi always rests on a character.

  • I: to activate insert mode or move to the start of your current line.

  • A: to move to the end of your current line.

  • i: to activate insert mode or begin typing before the character your cursor is placed.

  • a: to type after the current character on which your cursor is positioned.

  • o: to type on a new line after the current line you are on.

  • O: to type on a new line before the current line you are on.

  • dd: to remove/cut an entire line regardless of the cursor position.

  • dw: to remove only one word.

  • <n>dd: to input figures to specify the number of lines you want to remove, write as 1dd, 2dd, etc.

  • D: to delete the current line from the character your cursor is on till the end of the line.

  • dG: to delete from the line you are on to the end of the file.

  • yy: to copy an entire line.

  • yw: to copy only one word.

  • <n>yy: to specify the number of lines you want to copy. Input it as 1yy, 2yy, etc.

  • ypp: to repeat the current line you are on.

  • u: to undo your last change.

  • .: to repeat your last command.

  • P: to paste before the current line on which you are located/cursor is positioned.

  • p: to paste after the current line you are on/ cursor is on.

  • /<word>: to search out a word. You write it as /what, /great, etc.

  • x: to delete a character that is under your cursor.

  • X: to delete the character preceding the cursor.

Conclusion

Throughout this article, you have learned about vi as a command line text editor in Linux and how to use it. Additionally, we have examined the various modes in vi, explored commands that can be utilized in vi, and performed basic actions. I sincerely hope you found this article helpful and enjoyed it.