{"id":20414,"date":"2016-10-14T07:47:32","date_gmt":"2016-10-14T12:47:32","guid":{"rendered":"https:\/\/www.rosehosting.com\/blog\/?p=20414"},"modified":"2022-12-05T06:55:22","modified_gmt":"2022-12-05T12:55:22","slug":"vim-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/vim-tips-and-tricks\/","title":{"rendered":"Vim Tips and Tricks"},"content":{"rendered":"
<\/p>\n
Today we will show you several 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.<\/p>\n
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 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: 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. Execute any command on the shell with: Example:<\/strong> Real life example:<\/strong> Revert it back with In its basic form, it is the Enter Blockwise visual mode with CTRL+V and mark the block you wish to comment. We have covered several Vim tips and tricks. 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.<\/p>\n:i<\/code>, and save it with
:w<\/code> or
:wq<\/code> (write to file and exit) or
:wq!<\/code> (! stands for do not prompt for confirmation). One other very common command is just `
o<\/code>` without a colon and when in COMMAND MODE which automatically puts you in INSERT mode and into a new line for writing. <\/p>\n
Vim Tips\u00a0<\/h2>\n
When you edit a file without sudo<\/h3>\n
\n:w !sudo tee % <\/code>
\nWill 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.<\/p>\nTravel back in time<\/h3>\n
\nHowever: :earlier 15m<\/code> will revert the document to how it was 15 minutes ago. The same command can take different variables for time, like
:earlier 5m<\/code>. You can revert the changes with its opposite command
:later<\/code>.<\/p>\n
Execute any command<\/h3>\n
\n:!<command><\/code><\/p>\n
\n:!ls -l<\/code>
\nWill minimize the Vim editor and put you into the shell with the output of the ls<\/code> command.
\nHowever :.!<command><\/code> (with the dot(.) before the !) will execute the command and paste the output into the current window.<\/p>\n
\n:.!date<\/code> And you will get the current date pasted into your document.<\/p>\n
Vim can also act like a hex editor:<\/h3>\n
:%!xxd<\/code><\/p>\n
:%!xxd -r<\/code><\/p>\n
Several useful delete examples\u00a0<\/h3>\n
\n
diw<\/code> to delete the current word and
ciw<\/code> to cut the current word.<\/li>\n
de<\/code> is like
diw<\/code>, however it opens the opportunity to delete every next word just by pressing dot(
.<\/code>).<\/li>\n
di(<\/code> delete within the current parents.<\/li>\n
di\"<\/code> to delete the text between the quotes.<\/li>\n
dab<\/code> delete around brackets.<\/li>\n
daB<\/code> delete around curly brackets.<\/li>\n<\/ul>\n
Several useful cut examples<\/h3>\n
\n
ciw<\/code> to cut the current word.<\/li>\n
ci\"<\/code> cut the word inside the quotes.<\/li>\n
ci(<\/code> cut the word in the parents.<\/li>\n
C<\/code> cut the rest of the line and enter INSERT MODE. This is very useful for cut and paste.<\/li>\n<\/ul>\n
Miscellaneous useful commands<\/h3>\n
\n
zz<\/code> Scroll down the screen to make the current line appear in the middle. Very useful to put some chunk of code in focus.<\/li>\n
%<\/code> finds and moves the cursor to the matching parentheses.<\/li>\n
:%TOhtml <\/code> Creates HTML version of the current document. (Try it, it is very useful).<\/li>\n
vim http:\/\/rosehosting.com\/<\/code> Vim can also open up URLs assuming they go directly to static HTML files.<\/li>\n<\/ul>\n
Search and replace<\/h3>\n
:substitute<\/code> command or
:s<\/code> 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.<\/p>\n
\n
:%s\/something\/something_else\/g<\/code> Find the word something<\/em> and replace it with something_else<\/em> in the entire document.<\/li>\n
:s\/something\/something_else\/g<\/code> Similarly like the before command. This one just replaces in the current line only.<\/li>\n
:%s\/something\/something_else\/gc<\/code> Note the
c<\/code>. It replaces everything but asks for confirmation first.<\/li>\n
:%s\/\\<something\\>\/something_else\/gc<\/code> Changes whole words exactly matching something<\/em> with something_else<\/em> but ask for confirmation first.<\/li>\n
:%s\/SomeThing\/something_else\/gic<\/code> Here the
i<\/code> flag is used for case insensitive search. And the
c<\/code> flag for confirmation.<\/li>\n<\/ul>\n
Comment out blocks of code<\/h3>\n
\nPress capital I and enter the comment string at the beginning of the line (# for bash, or \/\/ for C++ etc..)
\nPress ESC twice and all the lines will be commented out.<\/p>\nConclusion<\/h3>\n
\n