Vim: The powerful and efficient text editor that you should know
No matter if you are developer, a sysadmin or a hacker, you have probably once heard about Vim and how it makes life easier when well mastered.
No worries if you have no idea about how to use it, because that is what we are going to talk and practice in this article.
What is Vim?
Vim is an open source command line based text editor created by Bram Moolenaar and released 1991. It is an improved fork of Vi, a text editor created in 1976 by Bill Joy. By the way, Vim is the short of Vi IMproved.
Now, see that we have a basic understanding of what is Vim, let’s explore the main differences between Vim and its predecessor Vi.
Differences between Vi and Vim
Here is a graph representing some of the biggest divergences between both text editors:
Well, after having a better understanding of the main differences between Vi and Vim, let’s learn how to use easily use it.
There we go :)
Hmmm, not yet guys :/
Indeed before learning how to use Vim, let’s quickly see Vim different modes which will will be helpful for the next sections.
Vim modes
Vim is composed of many modes. We will be focusing on the well-known ones.
Among the well known modes, we can list:
Normal mode
This is the default mode when we execute Vim.
This mode allows us to perform actions like navigation, text substitution, copy, paste, delete or cut text.
Insert mode
As its name suggests, this mode is used to enter text in our file.
To enter insert mode from the normal mode, you only need to use an insert command. One of them is ‘ i ’ which will switch vim to insert mode.
After entering this command, you will be able to edit your file the way you want.
In general, when switching from one mode to another, you may notice at the bottom left of your screen things like: __ INSERT __ for insert mode, __ REPLACE __ for replace mode, __ VISUAL __ for visual mode.
Visual mode
By default, Vim does not support mouse (everything is done using keyboard).
Nevertheless, sometimes we need to select a text or a block of text to perform some actions such as delete, copy or paste it somewhere in our file.
Luckily for us, we can solve this issue by utilizing the visual mode.
Simply put, the visual mode is a mode used for text selection.
To enter the visual mode, you must first be in normal mode, then press ‘ v ’. That’s it, you can now copy, delete a text from this mode.
Command mode
This mode is used to enter commands not supported by other modes.
To enter command mode, you must be first in normal mode, then press:
:
for command executions
:?
or / for pattern search commands
!
for filter command
A well known command is :wq
which is used for saving and exiting a file.
Replace mode
This mode is used to replace one or more occurrences of a specified string.
To enter replace mode, you must first be in normal mode, then press ‘R’ (in uppercase, otherwise you will be able to only change one character but not a string).
After that, you can replace a character or a word from the cursor current position.
Well, so far we have talked about some of the various modes of Vim namely the normal mode, the insert mode, the visual mode, the command mode and the replace mode.
Now, it’s time for hands-on examples.
On your marks, get set, let’s gooo :)
Vim commands
In this section, we are going to dive into some Vim useful commands.
To make things crystal clear for you, i decided to divide it into five sub sections.
Two important things to keep in mind are that, most of the commands we are going to use in our different modes depend on the cursor current position.
Also you can precede each command by an integer n in that way: nCmd
with n an integer and Cmd a Vim command.
Basic commands
In this section, we are going to see some must know commands, used for the rest of our work.
Install Vim
Sometimes, Vim may not be installed on your system.
In such circumstances, the first thing you need to do is installing it.
Depend on your GNU/Linux distribution, you can install Vim as follows:
Debian based distributions: sudo apt install vim -y
RPM based distributions: yum install vim -y
After successfully installed Vim, let’s execute it!
Run Vim
To run Vim, you can enter in your terminal:
Syntax: vim [options][file_path]
Note: It is not required for file_path to exist before using Vim.
You might notice the word [New] at the bottom left of the above image.
As the name suggests, this word means that Vim open a new file to edit.
Now, let’s write some text in our file. To do that, press :i
, then enter the text that you want to add in your file.
Once finished, press theesc
key.
Well done! Probably you are wondering now how to leave Vim!
Well, all you need is to press ctrl+c 🙃!
Ohh i am afraid, i was mistaken😜. In fact to leave Vim, you can use:xq
, :q!
,ZZ
or ZQ
.
Feel free, to take a look at the summary table below which sums up all the points we mentioned in this section.
Brilliant👍! Now that you know Vim basics, let’s move on to other Vim interesting commands.
Normal mode commands
As you may know, normal mode commands allow us to navigate in our file, substitute, copy, paste and delete a text or block of text.
Navigation
Navigation commands are used to move from bottom to top, left to right, one paragraph to another paragraph and so on.
Let’s take a bit look at the table below which is a summary of Vim navigation commands.
Examples:
Let’s now practice by executing some navigation commands.
To do that, open a non empty file that you have already created by entering into your terminal the command: vim your_file_name
Then, enter the following commands to see how it works.
2j
: move eight lines down
4b
: move two words back
6}
: move six paragraphs down
8l
: move to the next eight characters
Note: It is possible to use the arrows keys instead of the h,j,k and l keys while moving in our file. Nevertheless, it is not Vim way of doing things.
Text substitution
To substitute text in Vim, we can use the commands mentioned in the summary table below:
Examples:
ro
: replace the selected current with o
3cw
: change the next 3 words from the cursor current position.
2S
: change the two next lines, then switch from normal to insert mode.
Yank and paste text
In this section, we’ll cover some commands used to yank and paste text in our file.
Hmmm, alright! But, what does yank exactly mean?
Yank is Unix terminology that is slightly different from copy.
Indeed with yank, our information is not stored in the clipboard contrary to copy. In fact the text is pulled into the buffer for later use.
Here are some of the commands that we can use:
Examples:
5Y
: yank the 5 next lines from the cursor current position.
7p
: paste the yanked text 7 times down.
y9w
: yank the next 9 words from the cursor current position.
Text deletion
Here are some commands use to delete text in Vim:
Command mode commands
To refresh your mind, command mode is used for pattern searching, command filtering and command execution.
Let’s talk about these 3 use-cases of the command mode.
Command execution
To use this mode, we need first to be in normal mode.
Then, press the colon key ‘:’ followed by the command you want to execute.
Let’s suppose that we have opened a file named f1 in Vim.
Then we can do some manipulations in this file using the command execution commands.
Also, we are gonna see things like enabling text highlighting, lines number and mouse in Vim.
Pattern Searching
As its name suggests, command mode pattern searching is utilized for searching specific patterns in a file.
Let’s take a quick look to how to do that in the summary table below.
You might notice that in some commands below, we used the colon notation like we did in execution commands.
The fact is that except the first command in the above table, all the remained commands are execution commands.
However knowing that they are used for searching and replacing a string, i decided to put them in the previous table in order to facilitate the understanding.
Examples:
For that, we’ll use the following pattern searching command:
# Replace all 'Vi' occurrences by 'Vim' in a file named f1
: %s/Vi/Vim/g
# Replace only the first occurence of Vi by Vim for the first twenty lines
:1,20s/.*Vi/Vim/
# Replace all Vi by Vim in every word that contains 'Vi' in the file
:1,$s/.*Vi/Vim/g
# Replace all 'Vi' occurrences placed at the beginning of a file by Vim
:1,$s/^Vi/Vim/g
Command Filtering
Command Filtering is another sub mode of the command mode that can be used for executing bash commands from Vim or unsaving changes made in a file.
Here are some ways of how to use it:
:!bash
will execute a initiate a new bash session. Once you end with the session, enter exit to quit it, then press enter to go back to Vim.
Well, that is all for the command mode different commands.
Now, let’s move on to the insert mode.
Insert mode commands
Simply put, insert mode is one the vim modes that allows us to insert text in our file.
Here are some commands used to insert text in a file:
For this section, i will let you test the commands below on your Vim editor knowing that it is pretty intuitive.
Visual mode
The visual mode can be handy when it comes to select a text or block of text in order to perform some actions like deleting, yanking , editing it.
To do that, we only need to press the ‘v’ key, then select our text using the navigation commands or the mouse if enabled.
After selecting our text, we can perform our actions using text substitution, text deletion or text yank commands that we early mentioned.
It is important to note that you must first be in normal mode before using the visual mode.
Replace mode
This mode is intuitive because all you need is to press the R key and then replace all the characters you want from the current cursor position.
Nevertheless, you need to be first in normal mode before pressing R key.
Let’s recap!
- Vi IMproved is a powerful and efficient command line based text editor that can be awesome when well mastered.
- Vim is an improved fork of Vi which had some limited features.
- Vim is composed of many modes. Some of them are the normal mode, the insert mode, the command mode, the replace mode and the visual mode. Each mode has its own set of commands to perform a given task.
- Vim can be intimidated when using it for the first time. Nevertheless, always remember that every start is not a breeze. You need to practice over and over again, make several mistakes, get frustrated a bit (yeah, yeah) before starting to really enjoy the power of this editor.
Well done guys 👏!
Hope you learnt something.
I do encourage you to not mind the difficulties that you may face when using Vim for your first time. Just keep improving your Vim skill day after day.
I promise you that you will rapidly see the changes.
Thanks for subscribing to my newsletter to keep up with my latest articles.