Written By:
Sonali7
5 minute read
ViLinuxEditorUnix

What is Vi Editor in Linux?

Posted in Facts     MAY 4, 2021

Are you also thinking about what the vi editors are and how to work in the vi editor? If yes, then don't worry because this article is for you. In this article, we will discuss what is a vi editor, its mode of operation, how to use it, its different editing commands, how to move within the file, and how to save it.

So, let's get started.

What is the VI editor?

The default editor that comes with the UNIX operating system is called vi. VI in the vi editor stands for Visual Editor. It is a user-friendly and very powerful application that is available in all Linux distros. An improved version of vi editors is vim.

Why VI editor?

These are the following reasons why VI editor is popular:

  • It is available in almost all Linux Distributions.
  • It is user-friendly.
  • It works the same across different platforms and distributions.

There are two modes of operation of VI editor:

  • Command Mode: In command mode, actions are taken on the file.
  • Insert Mode: In insert mode, the text will be inserted into the file.

By default, the vi editor starts in the command mode. You have to be present in the insert mode in order to enter the text. For that, just type 'i' and you'll be in insert mode.

vi Command mode:

When vi starts up, it is in Command Mode. These are the following features of Command Mode:

  • This mode can only understand commands.
  • It allows you to move the cursor.
  • It allows you to cut, copy, paste, or delete a piece of text.
  • These commands are case-sensitive.
  • It saves the changes you have made to the file.

vi Editor Insert mode:

This mode allows you to insert text into the file. These are the following features of the insert mode:

  • To come in insert mode you simply press I on the keyboard.
  • Now, whatever you type in this mode is interpreted as input and finally, it is put in the file.
  • Press the Esc key, to get out of the insert mode and back to command mode.

How to use vi editor?

These are the following steps that are to be followed in order to create a new file:

  • Create a new file. Use the below syntax to do the same.
    vi <filename_NEW>
  • Now the vi editor will open in the command mode.
  • Press i to enter into the insert mode.
  • Add content in the insert mode.
  • Press ESC to enter into command mode.
  • Press :wq to save and quit.
  • Now, the content files can be checked.

VI Editing Commands

These are the following VI Editing commands along with their description:

Command Description
i Insert at cursor (goes into insert mode)
a Write after the cursor (goes into insert mode)
A Write at the end of the line (goes into insert mode)
ESC Terminate insert mode
u Undo last change
U Undo all changes to the entire line
o Open a new line (goes into insert mode)
dd Delete line
3dd Delete 3 lines.
D Delete contents of the line after the cursor
C Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dw Delete word
4dw Delete 4 words
cw Change word
x Delete the character at the cursor
r Replace character
R Overwrite characters from cursor onward
s Substitute one character under cursor continue to insert
S Substitute the entire line and begin to insert at the beginning of the line
~ Change case of individual character

Note: Commands in VI editor are case-sensitive, so it is important to use the commands in the right letter-case.

Moving within a file

It is important to be in the command mode in order to move within a file. These are the following default keys for navigation:

  • k - Move cursor up
  • j - Move cursor down
  • h - Move cursor left
  • l - Move cursor right

Or the arrow keys on the keyboard can also be used for navigation.

Saving and Closing the file

It is important to be in the command mode in order to exit the editor and save changes to the file. These are the following ways to save a file:

  • Shift+zz - Save the file and quit
  • :w - Save the file but keep it open
  • :q - Quit without saving
  • :wq - Save the file and quit

Summary

Let us summarize the above-discussed points for our better understanding:

  • The vi in VI editor stands for Visual Editor.
  • It is installed in every Unix system.
  • It is available in all Linux Distributions.
  • There are two modes of operation in the vi editor: Command Mode and Insert Mode.
  • When vi starts up, it is in Command Mode.
  • Command mode is responsible for taking users' commands.
  • Now, to enter into the insert mode press i.
  • The insert mode is responsible to take the entered text.

   IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS