How to use Vi editor in Linux (with examples)

In this tutorial, we shall show you how to work with the Vi editor and the most important shortcuts to use that will help you effectively edit any file. Vi has three main modes that you can use including the Command mode, Escape mode, and the Insert mode that we will be discussing today.

Vi editor is short for the visual editor app and is a command-line based tool used as the default editor in almost all Linux distributions. It is similar to Notepad on Windows or TextEdit on macOS.

In this tutorial, we shall show you how to work with the Vi editor and the most important shortcuts to use that will help you effectively edit any file. Vi has three main modes that you can use including the Command mode, Escape mode, and the Insert mode that we will be discussing today.

1. Command mode

The command mode in the vi editor is the first mode you will get when you open up the vi editor. In this mode, every typed character is considered as a command. Hence, the vi editor will not display or show any of those commands in the window. Moreover, as long as you are in this mode, you can copy, paste, move through the file, or even delete some characters from the file. Additionally, if you are using any of the other two modes and need to return to the command mode, all you need to do is just press the [ESC] key from the keyboard.

To open a file in the vi editor, launch the Terminal and enter the following command:

vi filename

Where the filename may be a new file that will be created or an already existing file.

Create and Open Vi Test File
Create and Open Vi Test File

If you created a new file that is not already exiting, then you should get something like the below screenshot:

The Vi Test File Opened
The Vi Test File Opened

You can use the following shortcuts:

  • dd is used to delete a line from the file
  • dw is used to delete a word from a file
  • x is used to remove a character from a file
  • G is used to go to the end of the file
  • Cursors are used to move and navigate within the file

2. Escape mode

To be able to use this mode, you need to first be in the command mode, then press the colon “:” character. Typically, during this mode, you can write any command next to the colon character. The forward slash can be used instead of the colon character, and that is in case you need to search the text in the file.

The following commands can be used during the escape mode:

  • :wq, is used to save and quit the file. Note that the command is case insensitive, and it appears at the bottom of the screen, as you can see in the below screenshot.

Save and Quit File
Save and Quit File

  • :q! is used to quit the file without saving.

Quit The File Without Saving
Quit The File Without Saving

  • :q is used to quit the file.

Quit The File
Quit The File.

  • :w is used to save the file without quitting it.

Save The File
Save The File

  • /string is used to search for the string in the file.

Search For a String
Search For a String

You can hit enter then the cursor will highlight the string you are searching for if it is found like the below screenshot:

The Result of Searching a String
The Result of Searching a String

3. Insert mode

In the insert mode, each keyboard keystroke will be dealt with as a text, and the vi editor will preview it on the screen as entered from the keyboard by the user. This is the mode where you will be allowed to start entering and inputting text into the file.

During the insert mode, you can use the following shortcuts:

  • [ESC] + i, is used to insert text
  • [ESC] + a, is used to append text
  • [ESC] + A is used to append text at the end of a line
  • [ESC] + r, is used to replace a character in the file

Conclusion

That’s all about the Vi editor and its usage in Linux. Hopefully, this tutorial helped you to get you ready to edit your files using the vi editor and effectively use its shortcuts.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,874FansLike
265FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

best apps for ubuntu
OK, this one is going to be a long one, so grab a cup of coffee and scroll through the best apps that we think are must-have for your Ubuntu PC. We have hand-picked each one of these considering the most common categories that suit an average Linux user.
Uninstall Programs in Ubuntu
Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.