Vi Commands

VI Editor with Commands in Linux/Unix Tutorial

The VI editor is the most well-known and well-respected of all the text editors that are part of the Linux family. The following is a list of some of the reasons why it is such a popular editor:

1) It can be obtained from the majority of Linux distributions.

2) It is functionally equivalent across all supported systems and distributions.

3) It can be navigated easily. As a result, millions of Linux users adore it and make use of it for all of their editing requirements.

There are now more complex versions of the vi editor available than ever before, the most popular of which is called VIM, which stands for Vi Improved. Elvis, Nvi, Nano, and Vile are some of the other ones that are there. Learning vi is a good idea because it has a large number of features and provides a wide variety of options for editing a file.

In order to use the VI editor, you will first need to become familiar with its many operation modes. They have two primary components that can be separated apart.

The following are some of the topics that will be covered in this tutorial:

vi Command mode:

The Editor of the VI
This mode is where the vi editor starts, and in this mode it can only understand commands.
You are able to move the cursor, as well as cut, copy, and paste the text when in this mode.
This option will additionally store any modifications that you have made to the file.
It is important to capitalise commands. It is important that you utilise the appropriate letter case.

vi Editor Insert mode:

The insertion of text into the file can be done using this mode.
By pressing the letter I on the keyboard, you are able to transition from the command mode to the Insert mode.
Once you have entered the Insert mode, any key that you press will be treated as an input for the document that you are presently working on.
You need to press the Esc key in order to exit the edit mode and preserve the changes you have made. This can be done by returning to the command mode.
Editing in the vi text editor
In order to start the VI Editor, –
Launch the Command Line Interface (Terminal), then type.

How to use vi editor

vi <filename NEW> or <filename EXISTING>
In addition, the editor will open the file for you to edit if you specify an existing file to be edited. In every other case, you can make a new file.

VI Editing commands

The VI Editor’s command set for VI Editing
I – Insert at cursor (goes into insert mode)
a – Continue writing after the cursor (goes into insert mode)
A: Go to the very last space on the line (goes into insert mode)
Pressing the ESC key will exit insert mode.
u – Undo last change
U will undo all of the changes that were made to the line as a whole. o will open a new line (goes into insert mode)
dd – Delete line 3dd – Delete 3 lines.
D – Remove all text from the line that follows the cursor
C will remove anything that is written on the line after the cursor and replace it with new text. To finish the insertion, press the ESC key.
dw – Delete word 4dw – Delete 4 words cw – Change word x – Delete letter at the cursor
r stands for “replace character,” and “r” stands for “overwrite characters from cursor onward.” s stands for “substitute one character under cursor continue to insert,” and “s” stands for “substitute entire line and begin to insert at the beginning of the line.” stands for “change case of individual character,” and “r” stands for “overwrite characters from cursor onward.”
Important: Before attempting to run any commands, ensure that you are in the “command mode.” Because the VI editor is case sensitive, you need to ensure that you are entering the commands using the appropriate letter case.

Moving within a file

Make sure you are pressing the appropriate command, as failing to do so could result in modifications being made to the file that are not wanted. Pressing a, A, and o in the appropriate sequence will also get you into the insert mode.

When moving about inside of a file, pressing k will move the cursor up, j will move the cursor down, h will move the cursor left, and l will move the cursor right.
To move around inside of a file, you will need to be in the command mode. You can also utilise the arrow keys on the keyboard in addition to the usual keys that are listed below; these keys are used for navigating by default.

Saving and Closing the file

Performing a save and then closing the file
Pressing Shift and zz will save the file and end the session:
w – Save the file while maintaining its open state:
q – Left without saving your work:
wq: Save the file and exit the programme.
In order to exit the editor and save your changes to the file, you will need to be in the command mode.

Summary:

An Overview of the VI Editor The vi editor is the most well-known and widely used text editor for the Unix operating system. It is typically included in all Linux distributions.
It operates in two distinct modes: Command and Insert. The Command mode is the one that processes user input, and the Insert mode allows users to edit text.
You need to be familiar with the commands in order to efficiently work on your file.
You will benefit from learning how to use this editor if you create scripts or edit files if you utilise it.