Thursday, March 12, 2015

Vim - Some useful commands

Saving file and exiting

":x"
is equivalent to:
":wq"
The only difference is when file is not changed, ":x" will not change file's modify time, but ":wq" will.

Basic calculator

In insert mode, you can use "Ctrl+r" and "=" to do some simple calculation, for example,
The result of 5 x 5 is
(Ctrl+r)=5*5

Save a file that you forgot open as root

This is quite useful. Whenever you open a file that you do not have permission to write to and make some changes, ":w" will not save the file. You can use:
:w !sudo tee %
to save it.

Delete inside markers

Something that I always wanted to be comfortable doing when I started using Vim: easily delete text between brackets or parenthesis. Go to the first marker and simply use the syntax:
di[marker]

So for example, deleting between parenthesis would be:
di(
then type another ")"

Jump to previous/next position

When editing a very big file, it is frequent to make changes somewhere, and jump to another place right after. If you wish to jump back simply, use:
Ctrl+o
to go back to where you were.

And similarly:
Ctrl+i

No comments: