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]
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
➌ 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.In the following image, you can see how the file is exited.
Similar Readings
- The “grep” Command in Linux [10+ Practical Examples]
- The “wc” Command in Linux [15 Practical Examples]
- The “sort” Command in Linux [16 Practical Examples]
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
➌ Now, hit the ENTER key.
➍ To insert text in the file1.txt file, tap the “i” button.
➎ 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
➐ 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.
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
➌ Copy the next command in the command prompt:
vi file1.txt
➍ 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
➑ 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.
Similar Readings
- The “nano” Command in Linux [13 Practical Examples]
- The “cut” Command in Linux [8 Practical Examples]
- The “jed” Command in Linux [8 Practical Examples]
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
➌ 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.
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
➌ Now, tap the ENTER button.
➍ 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.In the following image, I have redone the last change of the text file called file1.txt in the Vi text editor.
Similar Readings
- The “vim” Command in Linux [8 Practical Examples]
- The “egrep” Command in Linux [10+ Practical Examples]
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
➌ 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
Output >
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.
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
- The “paste” Command in Linux [6 Practical Examples]
- The “split” Command in Linux [6 Practical Examples]
- The “sed” Command in Linux [7 Practical Examples]
- The “tr” Command in Linux [6 Practical Examples]
- The “uniq” Command in Linux [6 Practical Examples]
- The “printf” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners