vim commands in Linux with examples
vim commands

vim commands in Linux with examples

Through this article, we will learn about vim commands. vi is an advanced plain text editor that is run through the command line to edit and create files in Unix-based operating systems. Through the vim commands article, we will try to clear you all the questions and doubts related to the vim text editor.

vim is an advanced version of vi. The vim text editor is the best way to edit and configure all plain text files in Linux. Vi editor is also known as a visual editor.

Install vim package in the system

The package of vi is already installed in a Unix-based operating system, if you want to use vim in Linux, then you can install it with the following command.

[root@simi ~]# dnf install vim -enhanced -y

Install Vim package in /linuxCentos 7

[root@simi ~]# yum install vim -enhanced -y

install VIM Text Editor command  package in in Linux and centos

How to Use vim commands

It is very easy to use vi and vim text editor in Linux, for that first you have to open the terminal and type vi filename.

If you want to use vim to improve the version of vi then you have to type the vim filename. In this text editor, you cannot use the mouse, you will have to use the command only with the help of the keyboard.

# vim filename

Open File in vim text editor command

Insert the matter in the file

As soon as the file is created with vim or vi text editor, a sign of ~ will appear at the beginning of the file, it means that this line is unused and you can write your matter here and at the bottom of the file you will see the name of the file. The name with which you have created your file.

We discussed 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.

To write something in the file or edit the file, you have to use the insert mode, for that, you press one of them o, Esc+i, Esc+s, Esc+a, key from the keyboard.

open file in  vim Linux

OR

write vim in the terminal and hit enter the press ESC button from keyboard +: e and create and save a file in your desire location.

Create and Open file in VIM Text Editor

:w Or : write The file will be saved but you cannot exit
:wqsave and exit from the file
force: !
:w! :q!
You can forcefully save the file by putting the exclamation mark and can also exit from the file.
: xsave and exit from the file
:q!forcefully exit from the file
:wq! forcefully save and exit from the file
:up OR :updateThe file will be saved but you cannot exit
save file in VIM Text Editor

Save And exit from file:

If you want to save and exit from file, you can also use đŸ˜¡ instead of :wq

save file in VIM Text Editor command in centos 8

Basic Navigation

From the picture given below, you can learn to use some navigation keys, like if you want to move one character down in the file, then you can use the down navigation key or the j character.

if you want to move one character up use the k character from the keyboard Similarly, use h to Move one character left, and for the right use the L key from the keyboard.

Basic navigation  key in VIM Text Editor

Navigate to Top and Bottom of the File:

If you want to edit a large file in vim and you want to go from top to bottom or bottom to top, then you can use the shortcut key.

:0 (zero)go to top of the file
gggo to the top of the file
1ggo to the top of the file
Ggo to the bottom file
:$go to the bottom of file
: line numbergo to the particular line number

Search Word and Character in the file:

If you want to search any word or character in a file using the vim text editor, then you should use some of the search options given below.

:/characterSearch character in the file
:/wordsearch word in the file

Search Character in file:

:/ f

To search a character in a file :/ After you write the name of the character you want to search in the file as you can see in the screenshot below we have searched the f character in the file.

Search Character in vim text editor command

Search word In the File:

:/ option

To search a word in a file :/ After you write the name of the word you want to search in the file as you can see in the screenshot below we have searched the option word in the file.

Search Word in vim text editor command
Search word in file

Display Line number in open file.

: set nu or: set numberdisplay line number in file.

If you open the file in vim, you will not see the line number. If you want to show the line in the file with the number, then you have to use the: set nu or: set number command.

It is very easy to use, for this you have to come to COMMAND MODE. you want to go to the command mode, you have to press the ESC key from the keyboard, then type the command: set nu or: set number and enter. Your entire file will appear with line numbers.

set Line Number in VIM Text Editor command

Hide Line Number :

:set no number or : set nonu : hide line number.

As we have seen above how to set line number in file and if you want to hide line number from file then you have to use :set no number or : set nonu : hide line number command. For example you can see the below screenshot

COPY, Cut, AND PASTE FILE MATTER

: ycopy
: yycopy a line
: ppaste
: dcut
: ddcut a line

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), or <ESC> line No + cc ( for cut text )

Move the cursor to that point where you want to paste the cut or copy matter press ‘p‘ for Paste whatever has been copied.

DELETE Some LINES :

To delete a line number in the Open vim file, move the cursor to that line and press continue dd.To delete multiple lines press <ESC> +Line number + dd

<Esc>+line number+dd

Create Password Protect Text Files through vim commands

if you want to Create Password Protected file in Linux/Centos/MacOS. first Open Linux Terminal through the system Dash or 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, the -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 : ******

save password protected  file in VIM Text Editor commnad

Enter Password and Confirm password, without password you can’t read file content. every time need a password to open the file.

Use the following command to open a password protected file every time you put the password in the file .

[root@simi ~]# vim filename

Conclusion

These Tutorials We learned about vim commands in Linux with examples Step By steps hope that now you are understood the vim text editor. If you have any doubt regarding the vim command then you can give your advice and also ask the question via comment.

The Main Difference Between MP3 and MP4

FTP Server in Windows 10

How to install WineHQ in Ubuntu 20.04

Wireless Speaker

WiFi Router

Rate this post

This Post Has 7 Comments

Comments are closed.