Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
As we all know, Linux is one of the biggest open-source Operating Systems in the world today. Along with this OS comes a default editor called Vi Editor. In this article, we will discuss Linux - Vi Editor in detail.
Let us first start with knowing what a Linux - Vi Editor is.
Linux - Vi Editor
Vi Editor is one of the most widely used and beloved Linux text editor. It is available in practically every Linux distribution we can find.
It has two modes:
Insert
Command
The ‘Command’ mode accepts user commands, whereas the ‘Insert’ mode is used to edit text.
Advantages of Linux - Vi Editor
This editor is really beloved. It is because of holding some great advantage. We shall take a look at those below.
1) We can find it in nearly all Linux distributions.
2) It is consistent and works throughout all platforms and distributions.
3) It's simple to use, and it is very user-friendly compared to others. As a result, millions of Linux users adore it and use it for editing.
Vi Editor Versions and Operation Modes
In recent times, more sophisticated and advanced versions of the vi editor have been available. We can tell that the most popular of these is VIM. It stands for Vi Improved. Elvis, Nvi, Nano, and Vile are among the other popular ones. Vi is feature-rich and provides limitless options for file editing. For this reason, it may be really wise to learn it.
If we want to work with the Vi editor, we need to get to know about its operation modes first. The operation modes can be divided into two ways.
The first one is Vi Command mode, and the second one is Vi Editor Insert Mode.
Vi Editor Command Mode
Let us have a look at Vi Command Mode. We will see what Vi Command Mode is all about with its features.
The Vi editor gets open in the Vi Command mode. We should also know that it only understands commands.
We should keep in our mind that the Commands in Linux Vi Editor are case-sensitive. So we should be careful at noticing which letter case we are using.
We can move the cursor while in this mode. We can also copy, cut and pests the texts.
When we make any changes to the files, this mode also saves the changes.
Vi Editor Insert Mode
Now we will read about Vi Editor Insert Mode. Let us see about Vi Editor Insert Mode and its simple features and characteristics.
We use this mode for inserting text in files.
When we are in the command mode, we can switch to the Insert mode. This can be done by pressing "i" on the keyboard.
After we are in Insert mode, any key we press will be used as input for the file you are presently working on.
If we want to go back to the command mode and save our changes, we have to press the "Esc" key.
How to Use Vi Editor
An important part of Vi Editor is knowing how to use it. Let us have a look at how we can use the editor.
First, we have to start the Vi Editor. To do this, we will open the Command Line Interface (Terminal). There we will type the following:
vi <Add_New_Filename_here>
or
vi <Add_Existing_Filename_here>
When we type the vi command in the terminal with a file name, the terminal will clear up. Immediately after this, the contents of the file will be shown. If no such file exists, a new file will be created. The completed file will be saved with the file name that has been mentioned.
If we specify a file that is already existing, then the editor will open it up for us to edit. Otherwise, we always have the option to create a new file instead of an existing one.
Working on Vi Editor
We will be looking at the working of Vi Editor in this section. The below image shows what pops up when we open a Vi Editor.
Let us simply enter “vi” in the Linux terminal to see the outcome.
If we just put the “vi” command as shown above in the Linux terminal, a file will be created. But if we go on to save it, it will show “No file name” given for the file as below.
Let us then press the “i” key to go to insert mode, and then type “WELCOME TO CODING NINJAS!” as below.
After writing the text, we now need to save and quit the file. Therefore, we will enter the command “:wq” after pressing the “Esc” key. But since we had not earlier given a file_name when opening vi, so it will show as “No file name” as before.
So, let us now learn how to quit this file. We can simply put “:q!” to exit the file without saving.
Let us learn how to create a file in Vi, including a file_name. For example, in the below image, we are creating a file with the name “newCodingNinjaFile” with Vi Editor.
The “newCodingNinjaFile” file is created as below:
We will press “i” to go into insert mode and write some text in the file.
After this, we can simply put “:wq”, which will help save and quit the file. We will again be back to our Linux terminal.
Vi Editing Commands
Now that we got an idea of using Vi Editor let us take a look at the Vi Editing commands. But before that, we show to keep a few things in mind.
We should be in "command mode" if we want to execute these commands.
We should be careful with the letter care of our commands. It is because the Vi editor is case-sensitive.
We have to be sure that the command we input is correct. This is because we might make unwanted changes to our files if we press the wrong commands.
We have the option to enter the insert mode by pressing the ‘a’, ‘A’, and ‘o’ keys as we need.
Let us look at some of the editing commands in a tabular form:
Commands
Working
i
Pressing "i" takes us to the insert mode. (This happens at the cursor)
a
Pressing "a" takes us to the insert mode. (This happens after the cursor)
A
Pressing "A" takes us to the insert mode. (This happens at the end of the line)
Esc
Pressing "Esc" Terminates the insert mode.
u
Pressing "u" undo the last change that is made.
U
Pressing "U" undo all changes in the entire line.
o
Pressing "o" takes us to the insert mode (This opens a new line)
dd
Pressing "dd" deletes the line.
3dd
Pressing "3dd" deletes 3, i.e., three lines.
D
Pressing "D" deletes the contents of a line. (Contents After the cursor gets deleted)
C
Pressing "C" deletes the content after a line in a cursor and then lets us insert new text. (We can press the "Esc" key to end the insertion mode.
dw
Pressing "dw" deletes a word.
3dw
Pressing "3dw" deletes three words.
cw
Pressing "cw" changes a word.
x
Pressing "x" deletes a character that is at the cursor.
r
Pressing "r" replaces a character.
R
Pressing "R" overwrites the characters (Overwritter from cursor onwards)
s
Pressing "s" substitutes one character that is under the cursor. (insert continues)
S
Pressing "S" to substitute the full line. (It starts to insert at the start of the line)
~
Pressing "~" changes the cases of the individual characters. (Example: A turns to a)
Moving Within the File
Sometimes we might want to move our cursor within a file. We can use the arrow keys on our keyboard for easy navigation. But there are other default keys to do the same:
h - We can use the ‘h’ key to move the cursor to the left.
l - We can use the ‘l’ key to move the cursor to the right.
k - We can use the ‘k’ key to move the cursor upwards.
j - We can use the ‘j’ key to move the cursor downwards.
We must take note that we have to be in command mode for moving within a file.
Saving and Closing the File
After creating, modifying and making the updates to our files, we have to save and close the file. There are combinations of keys we can use to do these directly.
:w - This saves the file, but the file stays open.
:q - This quits the file, but the file is not saved.
Shift+zz - Pressing this saves the file and then quits.
:wq - This combination saves the file and then quits.
We have to remember that for exiting the editor and saving changes to the files, we have to be in command mode.
Frequently Asked Questions
What is meant by the Vi Editor in Linux?
Vi Editor means visual editor allowing the user to create a file, read a file or modify the already existing file.
What are the types of editors in Linux?
In Linux, there are two different categories of text editors, as follows: (i) Text editors for command-line, including Vi, nano, pico, etc. (ii)The second category is GUI text editors that include Kwrite, gedit (for Gnome), etc.
How should one use Linux - Vi Editor?
We have to enter “vi <file_name>” to create a file. By default, the file will be in command, read-only mode. We have to enter the “i” key to be in the insert mode to write in the file. After writing, press the “:wq” keys to save and quit the file.
Name the modes present in the Linux - Vi Editor and which is the default?
There are two modes present in Vi Editor, namely, ‘Insert’ mode and ‘Command’ mode. The command mode is the default mode in the Vi Editor of Linux.
Are vi and vim different?
Vi is the Linux family's primary and original full-screen text editor, originally built for UNIX platforms. Vim is a more powerful version of the vi editor. Vim is short for 'vi improved'. Vim commands and syntax are completely compatible with vi.
Conclusion
This article covered the basics of Linux - Vi Editor, including its two modes, i.e., Insert and Command mode. We discussed all the basic commands in Vi Editor and the working of Vi Editor.
If you would like to learn more similar to this topic, check out our related articleson-