In this Tutorials We Learn about vim commands in linux with examples. Basic And Advance in Linux/CentOS. VIM is a text Editor that that’s included in Linux, BSD, and macOS. vim is an Improved version of VI. it can be used to Edit All kinds of text files.
(1) Install Vim package in Centos 8:
[root@simi ~]# dnf install vim -enhanced -y
Install Vim package in Centos 7:
[root@simi ~]# yum install vim -enhanced -y
(2) open file from Command line:
# vim filename
[root@simi ~]# vim data
write vim in terminal and hit enter the press ESC button from keyboard +: e location of the file
[ root@simi ~] vim filename
saving a file:
|:w Or : write||The file will be saved but you cannot exit|
|:w ! Or : write !||the file will be saved with overwriten option|
|:up OR :update||The file will be saved but you cannot exit|
Save And exit from file:
|: x||save and exit from the file|
|:q!||exit from the file without exit|
|:wq||save and exit from the file|
We discussed about Basic three mode:
NORMAL MODE :By defaults vim stated in Normal mode Other modes can be accessed from normal mode only.
INSERT MODE: In this mode, anything can be written to the file. To write something in insert mode press o, Esc+i, Esc+s, Esc+a, and insert the key from your Keyboard.
COMMAND MODE: This mode is for executing commands at the bottom of the editor, To switch from normal mode to command mode type: then type the command For example wq.
Basic Navigation :
Navigate to Top and Bottom of the File:
Jump from beginning line to bottom of the line by shortcut.
|:0 (zero)||go to top of the file|
|gg||go to the top of the file|
|1g||go to the top of the file|
|G||go to the bottom file|
|:$||go to the bottom of file|
Search Word and Character in file:
|:/f||name of the character which you want to search ( f is a example of search character)|
|:/word||name of the word which you want to search ( Option is a example of search word)|
Search Character in file:
:/ f Search forward for the pattern
Search Character In File:
:/ option Search forward for the pattern
Display Line number in open file.
: set nu or :set number : display line number in file.
Hide Line Number :
:set no number or : set nonu : hide line number.
COPY AND PASTE
The line number from which to copy and cut the file move the cursor to that point and press <ESC> line No + yy (for copy text), <ESC> line No + cc ( for cut text ) Move cursor that point where you want to copy.
<Esc>+ line number + yy copy a line, press ‘p‘ for Paste whatever has been copied.
CUT AND PASTE
<Esc>+line number+cc cut a line, press ‘p‘ for Paste whatever has been copied.
DELETE Some LINES :
To delete a line number in Open vim file, move the cursor to that line and press dd twice.To delete multiple lines press <ESC> +Line number + dd
Create Password Protect Text Files
to Create Password Protected file in Linux/Centos/MacOS. first Open Linux Terminal through the system Dash or the press Ctrl+Alt+T from Your Keyboard.
To create a password protected file, follow the below command.
[root@simi ~] vim -x filename
When creating a password protected file with the above command, -x switch Indicating that you are protecting the file with a password.
Warning: Using A weak encryption method; see: help ‘cm’ Enter encryption key : ******* Enter same key again : ******
Enter Password and Confirm password, without password you can’t read file content. every time need password to open file.
Use the following command to open a password protected file.
[root@simi ~]# vim filename
This Tutorials We learned about vim commands in Linux with examples Step By steps hope that now you are comfortable with vim. If your problem is still not solved share your Problem via comments.