Some VIM tips and tricks.

Posted on

<esc> :wq! Save the file and quit vim

<esc> ZZ Other way to save the file and quit vim

<esc> :earlier 15m Go back 15 minutes in the history of the file

<esc> :later 15m Reverses the :earlier command

<esc> :! ls Run command ls from inside vim

<esc> :.! ls Run command ls from inside vim and write output into open file.

<esc> :w !sudo tee % > /dev/null With this handy shortcut you can save a file as root even if you opened vim as a non-root user. This works because vim redirects the file content to standard input of the command you specified after :w!

In this case this is sudo tee % > /dev/null. The sudo command runs the specified command as root and redirects the standard input to the command.

Vim replaces the % character with the file path of the open file.  The command tee reads from the standard input and saves everything to the file which is provided as the first argument. Because we run tee as root it can overwrite the file with root rights and save it.

It also prints everything again to the standard output. This is why we use > /dev/null at the end to suppress tee from printing everything to the terminal.

Vim will warn you that the file has changed because of the external overwrite from tee. It will the provide you with the option to re[l]oad or ignore the change.

dd Cut line into the the buffer.

Paste from the buffer

_dd Removes line without putting it into the buffer

10j Go ten lines down

10k Go ten lines up

10h Go ten lines left

10l Go ten lines right


Leave a Reply