Vim tips and tricks that you probably never heard of

The most common file editor in Linux is Vim. That’s right, you can like it or hate it, but it doesn’t matter. It can be found in every Linux, that is out there. Even if there is no other editor installed in the system, you can still use it in its most basic form, Vi, the program which is its predecessor, written by Bill Joy in 1970 for the UNIX operating systems. Vi and Vim are similar in many ways, with the exception that Vim (VI IMproved) was later developed and adjusted to the more modern Linux systems.

People often ignore Vim and find it frustrating. Most of them, know only how to exit from it, :q!. Those of you that know little about Vim is that when you open a text file you can edit it with :i, and save it with :w or :wq (write to file and exit) or :wq! (! stands for do not prompt for confirmation). One other very common command is just `o` without a colon and when in COMMAND MODE which automatically puts you in INSERT mode and into a new line for writing.

In the following text, I will describe some tricks that most people probably don’t know about.

When you edit a file without sudo

If you happen to edit a file without sudo, you will get a permission error while trying to save the file. So you have to save it in another file and then make the changes again. However, the command:
:w !sudo tee %
Will save the file without the need to do all of that. Of course, you still need to be a sudo user to use that command.

Travel back in time

Revert the document back into a specific point in time. In its most simple form just ‘u’ in COMMAND MODE will undo the last change, or CTRL-R will redo the changes.
However: :earlier 15m will revert the document to how it was 15 minutes ago. The same command can take different variables for time, like :earlier 5m. You can revert the changes with its opposite command :later.

Execute any command

Execute any command on the shell with:

:!ls -l
Will minimize the Vim editor and put you into the shell with the output of the ls command.
However :.! (with the dot(.) before the !) will execute the command and paste the output into the current window.

Real life example:
:.!date And you will get the current date pasted into your document.

Vim can also act like a hex editor:


Revert it back with :%!xxd -r

Several useful delete examples

Several useful cut examples

Miscellaneous useful commands

Search and replace

In its basic form, it is the :substitute command or :s for short that searches a text pattern and replaces it with a string. The command has many options and these are the most commonly used ones.

Comment out blocks of code

Enter Blockwise visual mode with CTRL+V and mark the block you wish to comment.
Press capital I and enter the comment string at the beginning of the line (# for bash, or // for C++ etc..)
Press ESC twice and all the lines will be commented out.


Vim is a very feature-full editor offering a plethora of options for all sorts of uses. Very often due to its complexity many people find it frustrating to begin with in the first place. However, once you begin to grasp its way of working, you will begin to realize that the options and features it offers are so unique that no other editor today can replace it.

Of course, if you use one of our Linux VPS Hosting services, you can always contact and ask our expert Linux admins (via chat or ticket) about Vim and anything related to Vim. They are available 24×7 and will provide information or assistance immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a reply. Thanks.

Categories: Tips and Tricks