DEV Community

Cover image for Each Dev Should Know Basic About VI Editor
N. R. Ashwin
N. R. Ashwin

Posted on

Each Dev Should Know Basic About VI Editor

What is VI ?

Visual (VI) is basic text editor work in the command based in Linux, which provide this functionalities as

  • Insert, delete, and modify text;
  • Copy words, lines, or blocks of text;
  • Search and replace characters.

Why should know ?

VI exist in the all the Linux and Unix based operating system, VI is very small and powerful text editor.

How to use ?

Before learn VI command we should know that VI work in three mode

  1. Command Mode
  2. Insert Mode
  3. Ex (Escape) Mode

Command Mode

This is the default mode when any file open and press Esc key to move in the command mode. In this mode all the key is interpreted as command. In this mode we can move cursor up & down, remove text, cut & past etc.

Insert Mode

In this mode we can only write the text and press i or a to move in the insert mode.

Ex Mode

Press colon : to switch in the Ex mode in this mode use to save & exit etc.

Note : To switch Insert Or Ex Mode first switch to the Command mode.

Basic command of the VI

To learn the basic command we follow the journey of the new file create and edit.

  1. Create the file vi file_path
vi /doc/new_file
Enter fullscreen mode Exit fullscreen mode

This create new_file on the path and open in command mode
Note : If new_file exist on the location then open without create new.

  1. Press i or a to switch insert mode. Now can write content as
Happy learning VI
Enter fullscreen mode Exit fullscreen mode

3.Press Esc and then : to switch the Ex mode
now can see the : bottom of the page
press w + Enter save file or
press wq + Enter save file & exit.

Basic command of the VI

Command Mode

Move cursor left:
h or left arrow

Move cursor right:
l or right arrow

Move cursor up:
k or up arrow

Move cursor down:
j or down arrow

Move cursor end of line:
$

Move cursor start of the line:
0

Delete the character at the cursor location:
x

Ex Mode Command

Save the file:
:w

Save file to another name:
:w file_name

Quit editing a file without saving:
:q

Quit editing a file that has been modified during the session but not saved:
:q!

Save the file and exit:
:wq or :x

Save from line n to line m in another file:
:n,m w file_n_to_m_data

Reload the last record of the file:
:e!

Paste the content of another file after the cursor:
:r file_name

Top comments (0)