How to Use vi Editor in Linux and vi Editor Command in Linux |
How to Use vi Editor in Linux and vi Editor Command in Linux
vi Editor :
. vi is the symbolic link of vim and this default editor comes with Linux based OS. This is very popular among sysadmins. This a very simple and powerful editor. You can use it to make your rivels list, a letter to your valentine, script, code, and edit any source code. This is your best friend in the Linux system.
vi Editor modes:
vi Editor has 3 modes –
1- Command mode :
This is the default mode when we enter in vi editor. In this mode almost every key n keyboard has a function, so use it very carefully.
2- Insert Mode :
You can enter this mode by pressing the “i” key on the keyboard. In this mode, you can write anything your code, list, or edit file.
3- Visual mode or GUI mode :
This is not available in all distro and less used feature of vi editor. You can enter this mode by pressing the “v” key on the keyboard from command mode.
Note: By default vi editor mode is Command mode. You can enter any mode from command mode only and escape from that mode to command mode by pressing the “esc” key on the keyboard.
These are the modes of vi editor in Linux. Below you will see some vi editor cheat sheet which you can use in your sysadmin work and give you more understanding of vi editor commands in Linux.
vi Editor Basic Commands :
In this section, we will talk about the basic command of vi editor which has to know by every Linux user for use of the basic function of vi editor.
Create New File or Enter Any Existing File :
To enter any file use command vi <filename>. For example, see below
# vi explinux
you will enter in command mode as default by this command.
Write or edit in vi editors:
Press lowercase i to enter in edit mode. Now you can write anything or delete and navigate by keyboard navigation arrow key.
vi Editor Save and Exit :
When you finished your editing you have to come in command mode for exit or save the file. Please find below the steps to save and exit from the file.
- Press esc key on the keyboard to change command mode from insert mode.
- write “:wq! ” in lowercase. Press enter key to execute the command to save and exit from file. Please check below pic to understand more.
w – stands for save file
q – stands for quit from file
! – do not ask anything just run my command
vi Editor Cheat Sheet :
Now you have learned how to use vi editor and basic commands of vi editor which every Linux user should have known. Now we will talk about some advance command which should be known by every Linux sysadmin or any advance user.
These commands will run only in command mode as mentioned earlier. By default vi is in command mode if you are in another mode press esc key to enter in command mode.
Linux is a case sensitive so be careful about it.
List of Commands :
Open or edit any file :
# vi <filename>
Enter to Insert mode
i
Enter in Command Mode:
esc key on the keyboard
Save the file and Continue Editing:
:w
Save and quit vi Editor :
:wq!
Quit from file and do not write change :
:q!
Yank :
Copy or yank line :
yy
Copy X number of lines :
Xyy
for example 3 lines – 3yy
Paste Yanked File Below the Cursor Line :
p
Paste Yanked File Above the Cursor Line :
P or shift+p
This is the yank function that can be used to reduce time. This is a very useful command.
Create a New Line below the current line of Cursor :
o
Create a New Line Above the Current Cursor Line:
O or shift+o
Go To End of Line and Switch To Insert Mode for Write:
A or shift+a
Switch To Insert Mode At Start Of Line:
a
Switch to Insert Mode at the Beginning of Line :
I or shift+ i
Move to Begin of the Word :
b
Move to End of the Word:
e
Go to the Last Line of Open file :
G or shift+g
Go to the X Number Line of Open file :
XG
for example, we have to go on 3rd line: 3G
Go to the X Number Line of Open file :
gg
Delete a Single Letter or Character:
x
Delete a Single Word in Open File:
dw
Delete X Number of Words in Open File :
Xdw
for example, delete 3 words: 3dw
Change Any Word in vi Editor :
cw
Delete an Entire Current Line :
dd
Delete X Lines in One Command :
Xdd
For example, we have to delete 3 lines then: 3dd
Change or Replace Any Character or Letter :
r
Overwrite All Characters form onwards Cursor Position:
R or shift+r
Get the Current Line’s line Number in Open File:
:num and press enter
Get All Lines’s Line Number in Open File:
:set nu and press enter key
Undo Last Change in vi Editor :
u
Undo Last Change in entire Line vi Editor :
U or shift+g
Search in vi Editor :
/<yourstring>
or
?<yourstring>
press n for the next result in an open file in vi editor .
Navigation In vi Editor :
Move left :
h
Move Down:
j
Move up :
k
Move right :
l
At this point, you are familiar with vi editor commands. Practice more to become pro at vi editor.
Summary :
- The vi editor is most popular and distributed in every Linux distribution to edit almost all types of files.
- There are 3 modes of vi editor
- In this article, we covered almost all commands of vi editor
Share Article if you like it or report any bug in comments
Pingback: 15 Linux Commands - Certainly Boost Productivity -