{"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":"
<\/div>

\"VIM<\/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 :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\"Vim<\/h2>\n

When you edit a file without sudo<\/h3>\n

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:
\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>\n

Travel back in time<\/h3>\n

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.
\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

Execute any command on the shell with:
\n:!<command><\/code><\/p>\n

Example:<\/strong>
\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

Real life example:<\/strong>
\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

Revert it back with :%!xxd -r<\/code><\/p>\n

Several useful delete examples\u00a0\"vim<\/h3>\n