Fun Facts — Few Vim Tricks

Tip 1 — Save file opened without write permissions

If vim is your default editor and in case you opened a file on which you modified something but suddenly realised that you don’t have the access permissions to edit the file. Then you got no option but to quit and then redo everything but this time using the sudo command. This has happened to me many times. We can still save the changes using the below vim command in vim command prompt.

Save file opened without sudo permission using “:w !sudo tee %“.

Let us check by modifying a read-only file

Original contents of a file which i have:

kasi@Vostro /Sourcefiles $ cat mnt_debug
mount -t debugfs none /sys/kernel/debug/
kasi@Vostro /Sourcefiles $ 

Let us open the file using vim editor without sudo command and see how it goes.


On the left bottom of the above image it says read-only. But i still want to edit and save. So i am going to copy the same line and paste it below.


Here after making the changes vim editor is showing a warning like a read-only file is being changed. Now it wont allow me to save. But using the already shared command we can save this file.


After giving the above command as in the image press enter then you will be prompted to enter the password then press enter to save the changes.

Now the contents of the file looks like this:

kasi@Vostro /Sourcefiles $ cat mnt_debug
mount -t debugfs none /sys/kernel/debug/
mount -t debugfs none /sys/kernel/debug/
kasi@Vostro /Sourcefiles $

We got this one.

Tip 2 — Revert the document to a particular time

The next trick is using time. This is like undo or redoing changes to file opened in vim but with time. This is very handy were we don’t need to take multiple copies of the same file.

To revert a file back to how it was 15 minutes ago we can use “: earlier 15m” in the vim command line editor. The time is mentioned in minutes.

To again go back to the original state we can use “:later” command in the editor.

Vim has a lot of tricks and it takes years to practice. I have shared here what was useful to me and more importantly these helped me save time while working.

Feel free to share your vim tricks in the comments section.