Vi Editor – Useful Commands

Last updated: November 19, 2022

Linux operating system comes with a few text editors. The most commonly used editor among them is vi / VIM and nano.

In this tutorial, we learn about vi / vim text editor and some of its useful commands.

What is vi editor in Linux

Vi is a text editor which available preinstalled in Linux/Unix operating systems. Using vi editor you can create a new file and edit any existing files. Most commonly used to edit programs or configurations file in Linux. If you look for more advanced features you can use VIM which is a vi-like editor.

In Linux, visudo and crontab default editor is vi. You can change the default editor by adding shell configuration in ~/.bashrc file.

The vi editor uses 3 operation modes: Command, Insert and Escape

Command mode:

In this mode vi editor accepts commands but won't display those commands on the screen. Commonly used to delete, copy or paste a line of text. Press ESC key to enter into Command mode. When you open vi editor it starts in Command mode.

Insert Mode (input mode):

Press the letter i from the keyboard to enter Insert mode. This mode is used to edit or insert text to the file.

Escape mode:

You can change to escape mode from command mode only. Press colon [:] key from the keyboard to enter Escape mode. Escape mode is used to type commands to save, quit and run commands etc.

How to open vi editor

The best way to open vi editor type vi filename from the command line.

vi text1.txt

This opens the existing file named text1.txt and if text1.txt doest exist then it creates a new file.

Vi Create a new file

Let check here how to to create a new file name file1.txt:

vi create file
create a new file
enter command mode
command mode

The symbol '~' indicates unused lines.

insert mode
Press 'i' to enter insert mode
add file contents
Add the file contents
save the file content and quit
Press Esc key and wq to save & quit

You can now view the file content using the cat command.

view file content
view file contents

Exit (quit) Linux vi editor without saving

To exit vi editor without saving the file content go through the following steps:

1. Press Escape key from your keyboard to enter command mode

2. Press colon [:] key from your keyword, now you will see a colon prompt followed by a flashing cursor at the lower-left corner of the screen.

3. Type the command q! and press Enter key. This will close the vi editor and all changes you made will not be saved.

Exit (quit) Linux vi editor with saving

To exit or quit vi editor with saving the file content go through the following steps:

1. Press Escape key from your keyboard to enter command mode

2. Press colon [:] key from your keyword, now you will see a colon prompt followed by a flashing cursor at the lower-left corner of the screen.

3. Type the command wq! and press Enter key. The w indicates that all changes will be written or overwritten to the file and q to close or exit the editor.

Useful vi editor Commands

Let's check some of the useful vi commands that you can execute in Command mode.

iChange to Insert mode and append text where the cursor points.
aChange to Insert mode and insert after the cursor.
AChange to Insert mode and insert at the end of the line.
oChange to Insert mode and insert at the new line.
ESCChange from Insert mode to Command mode.
dd Delete one line the cursor is on.
dwDeletes from the current cursor location to the end of blank delimited word.
4ddDelete 4 lines
uUndo last change
xDelete one character at the current cursor position.
GGo to the end of the file. ie the last line in the file
4GGo to the 4th line of the file.
ggGot to the first line of the file.
bThe cursor goes to the beginning of the word.
eThe cursor goes to the end of the word.
yyCopy the entire line.
3yyYank 3 lines.
pPaste the copied text after the cursor.
PPaste the yanked text before the cursor.
hMove the cursor left by one character position.
lMove the cursor right by one character position.
jMove the cursor down by one line.
kMove the cursor up by one line.

Using Control Key you can run some useful control commands for Window scrolling:

  • CTRL+f - Scroll one screen forward.
  • CTRL+b - Move one screen backward.
  • CTRL+d - Scroll down half screen.
  • CTRL+u - Scroll up half screen.
  • CTRL+g - Display total number lines and position of the cursor in % at the bottom of the screen.

Vi can perform search and replace text very efficiently from Escape mode. Let's check some escape mode capabilities:

Search a text:


This will find forward for the string 'tom" and highlight when it finds the first match. This search matches both the whole word and the pattern within any words. You can use 'n' to search the next occurrence and for the backward press 'N'.

Search and Replace:

Search for the first occurrence of the string 'tom' in the current line and replace it with the string 'tomorrow'.


The string search in vi editor is case sensitive by default. You can use the i flag to ignore the case.

Add % symbol, in the beginning, to search and replace the string in the entire file.


Show line numbers:

To display line number in vi editor:

:set number

To hide the line number:

:set nonumber

Display vi/vim color scheme:

:colorscheme [space] [crtl+d]

Change color scheme:

:colorscheme darkblue

Display the line number of the current line:



The vi editor is present on almost all Linux/Unix systems. It is very powerful as with a few short commands you can make changes to large files. In this tutorial, we learned about vi editor and its important commands.

Thanks for reading. Please leave your feedback and suggestions.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment