Vi editor is a command line text editor. It’s a single window with text input and output only. It is the most popular and classic text editor in the Linux and it is available in all Linux Distributions.
There are lots of commands in Vi editor but in this article, you will know the basic operation of Vi editor. Basic operations include create/open, edit, delete and save. Start your terminal first to enter commands.
Create/Edit file in Vi editor:
Syntax: vi <New_filename> or <EXISTING_filename> Example : vi myfile
This creates a new file if file does not exists, otherwise opens an existing file.
Now your Vi editor has opened. Before start editing, lets understand operation modes of Vi editor which are very important to understand first.
- Command mode: By default, Vi editor opens in command mode. This mode allows us to move through a file, and to delete, copy, or paste lines or words. In this mode, whatever you type is interpreted as a command and not shown on screen.
- Insert mode: This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and placed in the file.
- Last Line Mode (Escape Mode): Last Line Mode is invoked by typing a colon [:], while Vi is in Command Mode. The cursor will jump to the last line of the screen and Vi will wait for a command. This mode enables you to perform tasks such as saving files, exiting etc. commands.
You can switch to the Insert mode from the command mode by pressing ‘i’ on the keyboard. To return to the command mode and save the changes you have made you need to press the Esc key.
Although, there are some other commands also to move to insert mode shown in below table.
Switch from command to insert mode
Start typing before the current character
Start typing at the start of the current line
Start typing after the current character
Start typing at the end of the current line
Start typing on a new line after the current line
Start typing on a new line before the current line
Now start writing text in file. I’ll explain more about editing file in the next section. You can just type some text in the file and once typing done you can press Esc to move to command mode for saving the file.
Save and Exit
Type the command from the following table according to your requirement and press enter then. See there is colon before every command and when you press colon you will be moved to last line mode to enter command. For example – to save and exit from Vi editor type :wq and press enter.
Save and quit
Save as fname
Save and quit
Quit discarding changes made
Save (and write to non-writable file)
Copy and Paste Commands
Press Esc to come out from insert mode. You can run these commands to copy/paste characters and lines in command mode.
Copies the current line.
Copies the current word from the character the lowercase w cursor is on, until the end of the word.
Puts the copied text after the cursor.
Puts the yanked text before the cursor.
Copy/Cut past multiple lines
Sometimes it is required to the block of lines. To achieve this follow the following steps:
- Position the cursor where you want to begin cutting/copying.
- Press v to select characters (or uppercase V to select whole lines).
- Move the cursor to the end of what you want to cut/copy.
- Press d to cut (or y to copy).
- Move to where you would like to paste.
- Press P to paste before the cursor, or p to paste after.
Moving within a file
You can either use arrow keys or following keys to move within a file.
Move cursor up
Move cursor down
Move cursor left
Move cursor right
You can delete text using normal backspace and delete button while in insert mode. In command mode, you can use the following commands. Most commands in Vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two characters under the cursor location and dd deletes two lines the cursor is on.
Deletes the character under the cursor location
Deletes the character before the cursor location
Deletes from the current cursor location to the next word
Deletes from the current cursor position to the beginning of the line
Deletes from the current cursor position to the end of the line
Deletes from the cursor position to the end of the current line
Deletes the line the cursor is on