VI editor or visual editor is an inbuilt text editor that is added by default with UNIX OS. Using the editor you can easily edit new or existing text files. If you are a new user and seeking all the information on how to use VI editor or run files on it we have got it all covered for you. Read the article thoroughly to find the basics of VI editor commands in Linux.
The text editor for the family of Linux VI Editor is one of the most popular, classic, and useful editing tool. The editor is available for users of different distributions and works on the same lines for all of them. The user interface is friendly which makes it a preference for millions of users to edit existing text files and create new ones from the scratch.
The latest available version of VI editor is VIM i.e VI improved. The editor has rich features and capabilities to edit files on your Linux machine. The first thing to be understood while learning about Vi editor is the modes of the editor.
The Linux vi commands can be carried out in the two modes of the editor. These modes are as follows:
The default mode of Vi editor is command mode. This mode can only understand the commands that are carried out. It enables users to move their cursor, cut, copy & paste text. Also, in the command mode, all the changes made in the file are automatically saved.
As the name signifies the mode is used to insert text into your file. To switch to this mode use the i key on your keyboard. As soon as you are in the insert mode any input will be added as text in the current file. As you finish inserting the text in your file use the Esc key to get back into the command mode in your editor.
Note: There’s an additional mode i.e Last line or Escape mode. This can be invoked by using the : (colon). As you enter the escape mode the cursor will move to the end of the screen & wait for user to enter the further command. This mode allows you to save files or perform similar executing commands in Vi editor.
Use any of the below-mentioned commands to open any existing file or to create a new one.
Press the Esc command twice to enter the command mode. This will help you to move within your file without affecting the text in it. Use the following commands to move around in the file at the pace of one character.
S. No. | Command | Description |
1. | k | Move cursor up by one line |
2. | j | Move cursor down by one line |
3. | h | Move cursor to one left to the character of the current position |
4. | l | Move cursor to one right to the character of the current position |
5. | 0 or I | Position the cursor to begin of the line |
6. | $ | Position the cursor to end of the line |
7. | W | Position cursor to the succeeding word |
8. | B | Position cursor to the preceding word |
9. | ( | Position the cursor to the starting of the current sentence |
10. | ) | Position the cursor to the starting of the next or following sentence |
11. | H | Move to the top of your screen |
12. | nH | Move to nth line from the beginning of the screen |
13. | M | Move to the middle of your screen |
14. | L | Move to end of the screen |
15. | nL | Move to nth line from end of your screen |
16. | Colon with x | Move the cursor to the line number x |
S. No. | Command | Description |
1. | Ctrl + d | Move forward half the screen |
2. | Ctrl + f | Move forward one complete screen |
3. | Ctrl + u | Move half screen backward |
4. | Ctrl + b | Move backward one complete screen |
5. | Ctrl + e | Move screen up by one line |
7. | Ctrl + y | Move screen down by one line |
8. | Ctrl + u | Move up the screen by half page |
9. | Ctrl + d | Move down the screen by half page |
10. | Ctrl + b | Move up the screen by one page |
11. | Ctrl + f | Move down the screen by one page |
12. | Ctrl + I | Redraw the entire screen |
In order to edit files you must be in the insert mode. Apply the desired command using the following descriptions for editing & inserting text in files.
S. No. | Command | Description |
1. | i | To insert text before the current location of the cursor |
2. | I | To insert the text at the starting of the current line |
3. | a | To add text after the current location of the cursor |
4. | A | To add text at the end of your current line |
5. | o | Add new line below the current location of the cursor |
6. | O | Add new line above the current location of the cursor |
7. | r | To replace one character under cursor with next entered character |
8. | R | Replace the text from cursor to the right |
9. | s | Replace one character with the desired characters. |
10. | S | Replace the entire line |
Use the below-mentioned Linux vi commands to remove or delete characters from your current file.
Use the following commands to copy text or entire line from one place in the file to another
Enter the commands given below after pressing the Escape key and then colon (:)
That is it. We have explained the VI editor commands available for all the distributions of Linux. The above guide will help you learn the features of vi editors in Linux in creating new scripts and editing your existing files.
If you need any further assistance feel free to leave your concerns in the comments box below. Also, don’t forget to share your valuable feedback and suggestions in the comments. Make sure you subscribe to our blog for more tech-related updates.
February 7, 2022
December 15, 2021
May 11, 2022