Vim tips and tricks that you probably never heard of

vim tips and tricks

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 :.!<command> (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

  • diw to delete the current word and ciw to cut the current word.
  • de is like diw, however it opens the opportunity to delete every next word just by pressing dot(.).
  • di( delete within the current parents.
  • di" to delete the text between the quotes.
  • dab delete around brackets.
  • daB delete around curly brackets.

Several useful cut examples

  • ciw to cut the current word.
  • ci" cut the word inside the quotes.
  • ci( cut the word in the parents.
  • C cut the rest of the line and enter INSERT MODE. This is very useful for cut and paste.

Miscellaneous useful commands

  • zz Scroll down the screen to make the current line appear in the middle. Very useful to put some chunk of code in focus.
  • % finds and moves the cursor to the matching parentheses.
  • :%TOhtml Creates HTML version of the current document. (Try it, it is very useful).
  • vim Vim can also open up URLs assuming they go directly to static HTML files.

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.

  • :%s/something/something_else/g Find the word something and replace it with something_else in the entire document.
  • :s/something/something_else/g Similarly like the before command. This one just replaces in the current line only.
  • :%s/something/something_else/gc Note the c. It replaces everything but asks for confirmation first.
  • :%s/\<something\>/something_else/gc Changes whole words exactly matching something with something_else but ask for confirmation first.
  • :%s/SomeThing/something_else/gic Here the i flag is used for case insensitive search. And the c flag for confirmation.

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.

Tips and Tricks
How to use traceroute in Linux
Tips and Tricks
How to Redirect HTTP Traffic to HTTPS in Nginx and Apache
Tips and Tricks
How to set up time zone on CentOS 7
  • sedlav


    Author Reply

    Very useful tips about VIM another ones (mainly for developer): autocomplete: Ctrl p or Ctrl n, guw and gUw change next words to lowercase and uppercase respectively.

  • A guy

    A guyA guy

    Author Reply

    The flag ‘i’ in :substitute is used for case-insensitive search. Please correct that in your article.

    • admin


      Author Reply

      Thanks. We have updated the article.