The “vi” Command in Linux [6 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The vi command is used to open up an editor called Vi in Linux. It is used to create and edit text files from the terminal command line in the Vi text editor. In this article, you will find the syntax, control commands, and how to use the Vi editor in Linux.

A. Description

The vi command is an easy-to-use command in Linux. Using this command, you can create text files in the Vi text editor and edit the files. With this editor, you can insert, delete, and modify text in Linux. Vi editor is widely used by system administrators and programmers around the world.

B. Syntax

The vi command has a simple syntax in Linux. And the syntax is indicated below:

vi [FILENAME]
Note: In the syntax above, FILENAME enclosed by a square bracket suggests that you can specify one file at a time.

C. Options

There are no options available for the vi command in Linux. Nevertheless, there are a lot of control commands available for the Vi text editor. I’ve outlined a few useful control commands for this editor below:

Vi Control Commands

  • i –Gets into the insert mode
  • o – Opens a new line in the insert mode
  • ESC – Switches to the command mode
  • D – Deletes the content of a line after the cursor
  • x – Deletes one character at the cursor
  • dd – Deletes the line
  • 3dd – Deletes three lines
  • dw – Deletes a word
  • 4dw – Deletes four words
  • cw – Changes the word
  • u – Undo the last change
  • U – Undo all changes of an entire line
  • R – Overwrites characters from the cursor onwards
  • r – Replaces a character
  • s – Replaces one character under the cursor and continues to insert
  • S – Replaces a full line and inserts at the beginning of a line
  • ~ – Changes the character’s case

How To Use Vi Editor in Linux

The vi command in Linux is used to open up a text editor called Vi. Here you will get to know how to use the Vi Editor using vi command in Linux. In this article, I have worked with the files below.

Example 1: Open New File & Exit Using the “vi” Command in Linux

You can open a text file in the Vi text editor using the vi command in Linux. I am opening a text file named file1.txt. To do this you can follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the next command in the command prompt:

vi file1.txt

Open New File Using the “vi” Command in Linux➌ Then, press the ENTER button.

➍ To switch to the command mode in the text editor, press the ESC button.

➎ To exit the text editor, Write the following command in the command prompt:

:q

➏ And finally tap the  ENTER key.

Output >

As you can see in the image below, a new text file called file1.txt is opened in the Vi text editor.Open New File & Exit Using the “vi” Command in LinuxIn the following image, you can see how the file is exited.Open New File & Exit Using the “vi” Command in Linux


Similar Readings


Example 2: Insert, Save & Exit File Using the Vi Text Editor With the “vi” Command in Linux

You can insert text inside a text file, save the file, and exit using the Vi text editor in Linux. I will open a new text file named file1.txt to insert text, save the file and exit the text editor. To do the same you can follow the process below:

Steps to Follow >

➊ Initially launch the Ubuntu Terminal.

➋ Type the next command in the command prompt:

vi file1.txt

Open New File Using the “vi” Command in Linux➌ Now, hit the ENTER key.

➍ To insert text in the file1.txt file, tap the “i” button.Insert Text Into File Using the Vi Text Editor With the "vi" Command in Linux

➎ For switching to the command mode in the text editor, strike the ESC button.

➏ To save the file & exit the text editor, Copy the following command in the command prompt:

:wq

Save & Exit File Using the Vi Text Editor in Linux

➐ Then, press the ENTER key.

➑ To check the contents inside the file, run the command below in the command prompt.

cat file1.txt

➒ After that, tap the ENTER button.

Output >

As you can see in the image below, the text is inserted in a new text file called file1.txt in the Vi text editor.Insert, Save & Exit File Using the Vi Text Editor in Linux

Example 3: Delete Text Inside the File in the Vi Text Editor Using the “vi” Command in Linux

You can delete the text inside a text file, save the file, and exit using the Vi text editor in Linux. Here I am using the text file named file1.txt to delete the text of the text file from the text editor. To do the same you can follow the instructions below:

Steps to Follow >

➊ To start open the Ubuntu Terminal.

➋ Write the following command in the command prompt and press the ENTER button.

cat file1.txt

Delete Text Inside the File in the Vi Text Editor

➌ Copy the next command in the command prompt:

vi file1.txt

Open New File Using the “vi” Command in Linux

➍ Now, hit the ENTER key.

➎ To switch to the command mode in the text editor, strike the ESC button.

➏ For deleting the text in the text editor, tap the “x” button.

➐ To save the file & exit the text editor, write the following command in the command prompt:

:wq

Save & Exit File Using the Vi Text Editor in Linux➑ Then, press the ENTER key.

➒ To check the contents inside the file, run the command below in the command prompt.

cat file1.txt

➓ After that, tap the ENTER button.

Output >

In the image below, you can see that the text is deleted from the text file called file1.txt in the Vi text editor.Delete Text Inside the File in the Vi Text Editor Using the "vi" Command in Linux


Similar Readings


Example 4: Move Around the Text In the Vi Text Editor

You can move the cursor around the text inside a text file using the Vi text editor in Linux. Here the text file I am using is named file1.txt. You can do the same by following the process below:

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Write the following command in the command prompt:

cat file1.txt

Move Around the Text In the Vi Text Editor➌ Now, hit the ENTER key.

➍ To switch to the command mode in the text editor, strike the ESC button.

➎ For moving around the text in the text editor, use the Arrow keys.

Output >

In the following image, you can see the cursor is moving around the text file called file1.txt in the Vi text editor.Move Around the Text In the Vi Text Editor

Example 5: Undo & Redo Inside the Text Editor

You can undo & redo the change inside the text file using the Vi text editor in Linux. Here I am using the text file named file2.txt. To do the same you can follow the following instructions:

Steps to Follow >

➊ To initiate, launch the Ubuntu Terminal.

➋ Copy the next command in the command prompt:

vi file2.txt

Open New File Using the “vi” Command in Linux➌ Now, tap the ENTER button.Undo & Redo Inside the Text Editor

➍ For switching to the command mode in the text editor, hit the ESC button.

➎ To undo the last change in the text in the text editor, use the “u” button.

➏ To redo the text in the text editor, use the dot “.” button.

Output >

In the image below, you can see the last change of the text file called file1.txt is undone in the Vi text editor.Undo & Redo Inside the Text EditorIn the following image, I have redone the last change of the text file called file1.txt in the Vi text editor.Undo & Redo Inside the Text Editor


Similar Readings


Example 6: Search a String Inside the Vi Text Editor

You can also search for a string inside the text file using the Vi text editor in Linux. Here I am searching inside the text file named file1.txt from the text editor. You can do the same by following the procedure below:

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Run the following command in the command prompt and press the ENTER button.

vi file1.txt

Open New File Using the “vi” Command in Linux➌ For switching to the command mode in the text editor, hit the ESC button.

➍ To search for a string inside the text file in the text editor, tap the “/” key and type the desired string just like below:

/Editor

Search a String Inside the Vi Text EditorOutput >

As you can see in the image below, I have searched the “Editor” string inside the text file called file1.txt in the Vi text editor.Search a String Inside the Vi Text Editor

Conclusion

As explained in this article, the vi command has significant use in Linux. You’ve also learned the syntax, some control commands, and the practical application of the Vi editor. Dive into the command and its practical applications thoroughly to become an expert user.


Similar Readings

5/5 - (2 votes)
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment