VI editor Commands in Linux/Unix [Complete Tutorial]

VI editor Commands in Linux/Unix [Complete Tutorial]

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.

What is a VI Editor

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.

Modes of Vi Editor

The Linux vi commands can be carried out in the two modes of the editor. These modes are as follows:

  • Vi Command Mode

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.

  • Vi Insert Mode

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.

How to Start Vi Editor

Use any of the below-mentioned commands to open any existing file or to create a new one.

  • Vi name_of_the_file: This enables users to open the existing file with the name or to create a new file if no file exists with the same name.
  • Vi -R name_of_the_file: Command used to open a currently existing file in the mode Read-only.
  • view name_of_the_file: Open an already existing file in the mode Read-only

How to Move Within a File

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

Using Control Commands

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

Edit and Insert Text in Files

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

Delete Characters:

Use the below-mentioned Linux vi commands to remove or delete characters from your current file. 

  • X : Remove the character before the location of your cursor
  • x : Remove character at the location of your cursor
  • Dw : Delete the current location of the cursor to the following word
  • d^ : Delete current characters from current position to beginning of line
  • d$ :  Delete current characters from current position to end of line 
  • Dd : Delete the entire line on which the cursor is

Vi Editor Copy Paste Commands

Use the following commands to copy text or entire line from one place in the file to another

  • Yy : Copy the current line
  • 9yy : Yank your current line & 9 lines below.
  • p : Adds the text that is copied after the current position of the cursor
  • P : Adds the text copied text before the current position of the cursor

Vi Editor Commands Save and Exit

Enter the commands given below after pressing the Escape key and then colon (:)

  • q : To quit the file
  • q! : Quit by discarding the changes
  • r fileName : Read the file with the name entered.
  • wq : Save & exit
  • w fileName : write to the file named next to the command. Works as the Save As feature.
  • w! fileName : Overwrite on the file.
  • !cmd : Run the shell command to return to the Command mode from insert mode

Closing Lines: VI Editor Commands in Linux/Unix

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.

By Support

Our aim is to chase the future, innovations, and the latest trends of all things tech

Write Comment
Deprecated: File Theme without comments.php is deprecated since version 3.0.0 with no alternative available. Please include a comments.php template in your theme. in /home/firmsexplorer/public_html/wp-includes/functions.php on line 5613

Leave a Reply

Your email address will not be published. Required fields are marked *