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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-01.png" data-lazy- height="422" src="data:image/svg xml,” width=”904″>

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

$ sudo apt install vim -y

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-02.png" data-lazy- height="342" src="data:image/svg xml,” width=”904″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-03.png" data-lazy- height="294" src="data:image/svg xml,” width=”904″>

Now installing the vim editor by the snap utility store.

$ sudo snap install vim-editor –beta

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-04.png" data-lazy- height="136" src="data:image/svg xml,” width=”904″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-05.png" data-lazy- height="94" src="data:image/svg xml,” width=”824″>

The file has been opened in the vim editor.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-06.png" data-lazy- height="422" src="data:image/svg xml,” width=”904″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-07.png" data-lazy- height="896" src="data:image/svg xml,” width=”732″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-08.png" data-lazy- height="428" src="data:image/svg xml,” width=”904″>

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-09.png" data-lazy- height="882" src="data:image/svg xml,” width=”726″>

The specified file will be open as output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-10.png" data-lazy- height="584" src="data:image/svg xml,” width=”904″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-11.png" data-lazy- height="880" src="data:image/svg xml,” width=”668″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-12.png" data-lazy- height="144" src="data:image/svg xml,” width=”778″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-13.png" data-lazy- height="884" src="data:image/svg xml,” width=”684″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-14.png" data-lazy- height="884" src="data:image/svg xml,” width=”660″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-15.png" data-lazy- height="894" src="data:image/svg xml,” width=”772″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-16.png" data-lazy- height="886" src="data:image/svg xml,” width=”694″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-17.png" data-lazy- height="340" src="data:image/svg xml,” width=”858″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-18.png" data-lazy- height="252" src="data:image/svg xml,” width=”846″>

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)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-19.png" data-lazy- height="326" src="data:image/svg xml,” width=”840″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-20.png" data-lazy- height="218" src="data:image/svg xml,” width=”848″>

Now we will press dd in the command mode.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-21.png" data-lazy- height="216" src="data:image/svg xml,” width=”904″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-22.png" data-lazy- height="222" src="data:image/svg xml,” width=”828″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-23.png" data-lazy- height="222" src="data:image/svg xml,” width=”822″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-24.png" data-lazy- height="278" src="data:image/svg xml,” width=”748″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-25.png" data-lazy- height="216" src="data:image/svg xml,” width=”798″>

The output, will be

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-26.png" data-lazy- height="228" src="data:image/svg xml,” width=”902″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-27.png" data-lazy- height="870" src="data:image/svg xml,” width=”884″>

On pressing V:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-28.png" data-lazy- height="888" src="data:image/svg xml,” width=”884″>

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!

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-29.png" data-lazy- height="882" src="data:image/svg xml,” width=”864″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-30.png" data-lazy- height="870" src="data:image/svg xml,” width=”882″>

If we press n:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-31.png" data-lazy- height="228" src="data:image/svg xml,” width=”840″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-32.png" data-lazy- height="414" src="data:image/svg xml,” width=”904″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-33.png" data-lazy- height="880" src="data:image/svg xml,” width=”888″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-34.png" data-lazy- height="70" src="data:image/svg xml,” width=”904″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/vim-editor-complete-guide-36.png" data-lazy- height="168" src="data:image/svg xml,” width=”904″>

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/hammad–150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.