11.6 C
Jaipur
Wednesday, December 8, 2021

How to use vim editor – Complete Guide

Must read

Vim editor is among the best editors used in the command-line interface to open, edit and save files, vim editor can be used for editing simple text files as well as different code files of different languages. Vim is the updated version of Vi editor that’s why vim is abbreviated as “Vi improved”. It is highly efficient and also known as IDE (integrated development environment) because of its popularity in the programming editors. Vim has different shortcuts to perform tasks, it has different modes and bindings.

This post will help us to know how we can use a vim editor for editing different files.

Comparison of Vim with Nano editor

Nano editor is the default editor in the Linux distributions whereas the Vim editor is mostly pre-installed in some distributions of Linux. There are some features on the basis of which it is more popular than nano and those features are:

Vim Editor Nano Editor
It is slightly complex for a beginner It is simple to understand for a beginner
Supports programming languages Does not support programming languages
It is a mode-based It is modeless
Improved version of Vi editor Improved version of Pico editor
Advanced editor with many tools Simple editor

Modes of Vim

Vim has two different types of modes, as

Command-line mode : When you open any file with vim, you are in the command mode by default. In command mode, you can perform different tasks by using the commands for example to delete a line, to copy the line, and to navigate the cursor in any specified position of the file. If for any reason you are not in the command mode, simply press the ESC key, to enter in the command mode.

Insert mode : To insert something, you have to choose the insert mode, for this purpose, simply press the I key to enter the insert mode. In this mode, you can write anything and can add anything to the file.  Once you are done by inserting, press ESC key from keyboard and switch insert mode to command-line mode.

Installation of Vim

In some of the distributions of Linux, vim is pre-installed but if it is not installed by default you can install it by two different methods either using the apt command or from the snap utility store. To find out, vim is installed by default or does not execute the following command in the terminal.

The output is showing it is not installed, so we will first install it using the apt command.

$ sudo apt install vim -y

To install it from the snap utility store, first, install snap utility.

Now installing the vim editor by the snap utility store.

$ sudo snap install vim-editor –beta

Vim commands

Vim has been installed in the Linux distribution. Now we will go ahead and learn about the commands in Vim. It has more than 1000 commands to perform different tasks efficiently. Different types of commands are explained with examples.

Basic Commands

Some basic commands of Vim editor are

Open a file : You can open a file with the vim editor by using the keyword “vim”. For understanding, we want to open a file, named file.txt using vim editor.

The file has been opened in the vim editor.

Help command : If you need any type of help regarding any command, type :help [command keyword] in the command mode, the list of the help will be displayed. For example, we find help regarding the “copy” command.

The output will be displaying a file containing all the relevant help regarding the keyword “copy”.

Open a code file : Now if you want to open any other file, you can open it by typing :e [file name] in the command mode. For understanding, we will open a file, named, code2.php using “:e code2.php”.

The specified file will be open as output.

Quit vim without saving a file : We can exit the editor without saving the changes we have done, by typing :qa or :q! and then press ENTER key. For example, we quit the file:

After hitting the ENTER, you will be back to the terminal.

Quit vim by saving the files : We can quit the file by saving it. To do so type :wq and press ENTER.

Save the file : While working if we want to save the changes, we can do it by typing :w and hitting the ENTER key.

Save a file by renaming : We can save a file by renaming it by typing “:w [file name]”:

Cursor navigation commands

In vim editor, there is no use of the mouse as the cursor is moved with the help of keys. We will discuss some keys and shortcuts to navigate in the vim editor. There is a list of keys and their purposes, use them by pressing semicolon “:” and then the specific key.

Commands Actions
h To move the cursor to the left position
l To move the cursor to the right position
j To move the cursor to the down position
k To move the cursor to the up position
M To move the cursor directly to the middle of the screen
L To move the cursor directly to the bottom of the screen
H To move the cursor directly to the top of the screen
e Places the cursor at the end of the word
b Places the cursor at the start position of the previous word
w Places the cursor at the start position of the next word
$ Places the cursor at the end position of the line
0 Places the cursor at the start position of the line
} Takes the cursor to the start position of the next block or next paragraph
{ Takes the cursor to the start position of the previous block or previous paragraph
) Moves the cursor directly to the start position of the next sentence
( Moves the cursor directly to the start position of the previous sentence
G Places the cursor at the end of the file
gg Places the cursor at the start of the file
# To go on a specific line, type the number of lines next to #
CTRL +  b Moves the cursor to one page back
CTRL + f Moves the cursor to one page forward

Editing commands

If we want to edit the text, first go to the insert mode by pressing the “I/i” key, then type the text. For editing purposes, there are some commands which can help in editing like copy, paste, delete and undo commands. We can discuss all these commands:

Copy commands : In vim, the copy word is derived from the word “yank” so it will use copy commands with a representation of yw.

Commands Actions
yy It is used to copy a line
yw It is used to copy a word
y$ It is used to copy from the present position of the cursor to the end of the line

Paste command : In vim, the copied text can be pasted by simply typing “p” after the semi-colon.

Undo command : In vim, if mistakenly or unintentionally any action has been performed, we can undo that action by typing “u” after the semi-colon. We made a blank line in the text as shown in the image below:

Now we will press the “u” key, by entering the command mode, by pressing ESC key after semicolon”:”,

Redo command : To redo any action in the vim, type “r” in the command mode (command mode can be opened by pressing ESC key after typing semicolon)

Delete commands : For the deletion of words or sentences, we use the commands described in the table.

Commands Actions
dd To delete a line
d To delete the selected portion of a line
dw To delete a word
D To delete a line from the present position of the cursor to the end of the line
dG To delete a line from the present position of the cursor to the end of the file
dgg To delete a line from the present position of the cursor to the start of the file

On pressing dd, the entire line has been deleted. For example, we open a file, named file.txt.

Now we will press dd in the command mode.

On pressing dw, “is” word has been deleted. For example, our cursor is on the word “is” in the first line.

On pressing D, the line is deleted from the cursor position:

On pressing dG, deleted all the lines from the starting position of the cursor:

On pressing dgg, previous lines from the cursor has been deleted:

The output, will be

Selection commands : For the selection or highlighting of the text, the following commands are used.

Commands Actions
v To highlight a character
V To highlight a line

On pressing :v.

On pressing V:

Show number against lines

Showing the numbers with every line, make it easy for us to determine which line we are and on which line we have to go for editing. To display the numbers along with the lines of text we can use any of the commands displayed in the table.

:set number
:set nu!
:set number!

Searching commands

We can search specific words in vim also like the other editors. The commands for searching are:

Command Actions
/ [enter the word ] Finds out the entered word from the entire file
? [enter the word] To search previous text from the entered word
n To search your word again in any direction
N To search the word again in the opposite direction

If we press n:

Word count commands

Like other editors, we can also count the words and characters in vim. For this, there are two ways, pressing g and then CTRL + G.

Or use w, !w, and -w in the command mode:

Compare files

We can compare two files in vim. The general syntax of the command will be

For explanation, we compare two files, file.txt and newfile using the vimdiff command.

$ vimdiff file.txt newfile

The output is showing differences like in the first line, letter “l” in first file is in upper case while in other file it is in lower cas and the last two lines are missing in the second file.

Conclusion

Vim editor is the command-line editor, which is much more versatile as it contains all the functions which are needed by the beginner as well as an expert and is popular for its different features. It is the advanced form of the Vi editor and it can be used to open programming files of different languages. In this article, we have discussed the installation and different commands of the vim editor which is used to handle the vim editor. We also tried our best to explain the use of commands with the help of examples.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article