Thursday 4 August 2022

THEORY - 5 :- VI Editor in Linux

 The VI EDITOR

VI is an editor that is fully in text mode, which means that all actions are carried out with the help of text commands. this editor, although it may appear of little practical use at first, is very powerful and can  be very helpful in case the graphical interface malfunctions.


The syntax to launch  VI is as follows.

viname_of_the_file

once the file is open, you can move around by using cursors or the keys h, j, k and l.

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.

Vi Modes

Vi has three operating modes.

Regular Modes :

This is mode you enter whenever you open a file. this mode allows  typing commands.

 

Insertion Modes :- 

this mode makes it possible to insert characters you capture inside of  the document. To switch to insertion mode, just press the key insert on your keyboard or ,by default, the key i.

 

Replacement Mode :- 

This mode allows you to replace existing text by   the text you capture. just hit r again to got o replacement mode and hit the key Esc to return to regular mode.

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 cutcopypaste, 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 in Linux?

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.

Basic VI Editing Commands

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

CommandDescription
iInsert at cursor (goes into insert mode)
aWrite after the cursor (goes into insert mode)
AWrite at the end of the line (goes into insert mode)
ESCTerminate insert mode
uUndo last change
UUndo all changes to the entire line
oOpen a new line (goes into insert mode)
ddDelete line
3ddDelete 3 lines.
DDelete contents of the line after the cursor
CDelete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dwDelete word
4dwDelete 4 words
cwChange word
xDelete the character at the cursor
rReplace character
ROverwrite characters from cursor onward
sSubstitute one character under cursor continue to insert
SSubstitute 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
  • - 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.

 


Popular Posts