How to Use the VI Editor in Linux

How to Use the VI Editor in Linux

If you’re searching for info related to the VI editor, this article is for you. So, what’s VI editor? VI is a text editor that’s screen-oriented and the most popular in the Linux world. The reasons for its popularity are 1) availability for almost all Linux distros, 2) VI works the same throughout multiple platforms, and 3) its user-friendly features. Currently, VI Improved or VIM is the most used advanced counterpart of VI.

To work on the VI text editor, you have to know how to use the VI editor in Linux. Let’s find it out from this article.

Modes of VI Text Editor

VI text editor works in two modes, 1) Command mode and 2) Insert mode. In the command mode, users’ commands are taken to take action on a file. The VI editor, usually, starts in the command mode. Here, the words typed act as commands. So, you should be in the command mode while passing a command.

On the other hand, in the Insert mode, file editing is done. Here, the text is inserted into the file. So, you need to be in the insert mode to enter text. Just type ‘i’ to be in the insert mode. Use the Esc key to switch from insert mode to command mode in the editor. If you don’t know your current mode, press the Esc key twice. This takes you to the command mode.

Launch VI Text Editor 

First, you need to launch the VI editor to begin working on it. To launch the editor, open your Linux terminal and then type:

vi <filename_NEW> or <filename_EXISTING>

And if you mention an existing file, VI would open it to edit. Alternatively, you’re free to create a completely new file.

VI Editing Commands

You need to be in the command mode to run editing commands in the VI editor. VI is case-sensitive. Hence, make sure you use the commands in the correct letter case. Also, make sure you type the right command to avoid undesired changes. Below are some of the essential commands to use in VI.

i – Inserts at cursor (gets into the insert mode)

a – Writes after the cursor (gets into the insert mode)

A – Writes at the ending of a line (gets into the insert mode)

o – Opens a new line (gets into the insert mode)

ESC – Terminates the insert mode

u – Undo the last change

U – Undo all changes of the entire line

D – Deletes the content of a line after the cursor

R – Overwrites characters from the cursor onwards

r – Replaces a character

s – Substitutes one character under the cursor and continue to insert

S – Substitutes a full line and start inserting at the beginning of a line

~ – Changes a character’s case

dd – Deletes the line

3dd – Deletes 3 lines

dw – Deletes a word

4dw – Deletes 4 words

x – Deletes a character at the cursor

cw – Changes the word

Commands for Moving within a File

For moving within a file, you should be in the command mode. Also, arrow keys can be used to navigate. Below are the commands used to navigate within a file.

k – Moves cursor up

j – Moves cursor down

l – Moves cursor right

h – Moves cursor left

Commands for Saving and Closing a File

To exit the text editor and save the changes to a file, you need to be in the command mode. Below are the commands to use for saving and closing a file in the editor.

Shift+zz – Saves a file and exits

:w – Saves a file and keeps the file open

:q – Exits without saving

:wq – Saves a file and quits


Through this article, we learned about the VI text editor. Here’s a summary of all the above facts discussed above. The VI text editor is the most used and popular Linux text editor. You can get it in almost any Linux distribution. There are two modes, Insert and Command. Command mode is used to take the user commands, while the Insert mode is used to edit text. In the earlier sections, we learned the commands used to work on a file. So, learning to work on the VI text editor will definitely give you advantages in the time of editing files and creating scripts.

Suparna is a freelance writer who writes about Linux including tips, tricks, and how-tos.

Load Disqus comments