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
: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.
: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
Execute any command
Execute any command on the shell with:
Will minimize the Vim editor and put you into the shell with the output of the
:.! (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
Several useful delete examples
diwto delete the current word and
ciwto cut the current word.
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.
dabdelete around brackets.
daBdelete around curly brackets.
Several useful cut examples
ciwto cut the current word.
ci"cut the word inside the quotes.
ci(cut the word in the parents.
Ccut the rest of the line and enter INSERT MODE. This is very useful for cut and paste.
Miscellaneous useful commands
zzScroll 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.
:%TOhtmlCreates HTML version of the current document. (Try it, it is very useful).
vim http://rosehosting.com/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/gFind the word something and replace it with something_else in the entire document.
:s/something/something_else/gSimilarly like the before command. This one just replaces in the current line only.
c. It replaces everything but asks for confirmation first.
:%s/\<something\>/something_else/gcChanges whole words exactly matching something with something_else but ask for confirmation first.
iflag is used for case sensitive search. And the
cflag 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.