Getting Started with Vim Editor in Linux

Reading Time: 4 minutes

Introduction

Vim is based on vi text editor. vim is a text editor that is upwards compatible to Vi. Bram Moolenaar was the developer of Vim text editor. Vim can be use to edit plain text. There are a lot of enhancements above Vi. Vim is an advanced and highly configurable text editor. It is widely use for being fast and efficient. Most importantly, It supports most of file types. This is reason which make this editor programmer’s editor.

Installation on Debian based Linux System

Installation is quite simple  and easy as compare to Windows on Linux platform. I am going to describe the step of installation on Debian based Linux.

Installation

First of all We need execute these command in terminal –

$ sudo apt-get update && sudo apt-get install vim

To ensure, Vim is correctly install or not. Execute below command −

$ which vim

It should print the location of Vim binary. In my case it was −

/usr/bin/vim

Vim Tutor

Vim editor has own tutorial. We can learn everything about that tutor page. You can see this tutorial by command vimtutor into the terminal .

$vimtutor

Example

Now, Let’s go with an example to use Vim

To open a file in vim editor, We just need to write the file name after the vim command in the terminal as follows:

$ vim example.txt

After execution of this command, a editor will open.

Let’s write some content into opened file.We need to go in insert mode to write something. To go into write mode type i  as follows:-

i

After going into insert mode you will see INSERT in the status bar. We can write any data in it.

Save and Exit:

We have written the info or data into a file now the task is to save and close. Now first of all we have to exit from insert mode by pressing the Esc key. After Esc, use semicolon to write command ( : ) and type the command wq!, hit ENTER.

:wq!

Exit without saving the file:

To exit from the file without saving, just use the command q! As follows

:q!

Moving the Cursor:

We can use the arrow keys to move the cursor into a file. As this is not recommendation to use arrow keys in vim. Vim provides the special key to move the cursor in the vim editor as below –

  • k   ->   move up                                            
  • j   ->  move down                                                  
  • h   -> move right                                                  
  • l   -> move left      

We can use all these option to move vim files and move the faster cursor into the files.

Exiting from Vim editor:

If you do not want to write anything in the file or exit without doing anything. Use the below command-

:q

To exit without saving changes. Use below command-

:q!

Use this command to quit and save the changes-

:wq

Text Editing: Deletion

We provide x key in command mode to delete the character under the cursor.

x

The character under the cursor will be delete.

Text Editing: Insertion

There are Four keys use for insertion text. Just type the key into the normal mode in vim.

  • i    ->   This insertion key is use to put the cursor before the current position.
  •  a  ->   This key is use to put the cursor after the current position.
  •  o  ->   This key is use to put the cursor below the line.
  •  O  ->  This key is use to puts the cursor above the line.

Motion:

Motions provide context to your Operators. These execute the action in a particular way.

Here is a list of some motions

  • w – until the start of the next word, EXCLUDING its first character.
  • e – to the end of the current word, INCLUDING the last character.
  • $ – to the end of the line, INCLUDING the last character.
  • We can use motion with the d key and with many more keys

Count:

Count is the number for which replete the motion for count number. Here is a demonstration of the use of count and motion

To move cursor 2 words forward use the below command

2w

Here 2 is the number of counts and w is use for word

To move cursor 4 line forward use the below command

4$

Deletion Commands:

Always use the Esc key to go into normal mode and use the insertion, deletion keys, and other keys. 

To delete the word move the cursor to the beginning of the word and use dw command in normal mode. The word under the cursor will be delete.

dw

As we know how to delete the word under cursor. Now we will delete the a word using below command.

Use below command to delete 2 words –

d2w

Delete the complete line, move the cursor to the beginning of the line and use d$ command. The line under the cursor will be delete.

d$

Undo and Redo:

As we are programmers, most time we are using undo and redo .vim to provide these to both features in it. To undo press u key in normal mode

u

To redo use the ctrl+r key in normal mode in vim

Ctrl+r

In this, We will learn how we can search a pattern in vim editor. We will use the below command to search pattern-

:/word

Now, Use n to move on next matching word

:n

Similarly, Use N to move on previous matching word

:N

Conclusion:

In this article, I have covered the most common commands. You can check the man page of vim for more by using this command man vim.

To know more about linux command follow this link https://blog.knoldus.com/basic-linux-commands-for-beginners/

Written by 

Mohd Muzakkir Saifi is a Software Consultant at Knoldus Software. He loves to take deep dives into cloud technologies & different tools. His hobbies are playing gymnastics and traveling.

1 thought on “Getting Started with Vim Editor in Linux10 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading