Useful Vim Tips
Vim is my favourite editor. I search for tips from time to time – and there are still nice surprises. After a few years of using it, it is high time to write down my own list of useful tips :)
Here is it:
- delete rest of line – to delete the rest of the current line press
Shift-D
(in the command mode). - repeat last command – to repeat the last command, press
.
(dot) (of course, in the command mode). - completion – saves typing and time esp. for longer words. Press
Ctrl-N
(in the edit mode) to get a word or a list of words that have the same prefix as the word on the cursor position. - path completion – great for bash scripts, images or CSS in HTML or Ant scripts.
Ctrl-X-F
(press and holdCtrl
, pressX
, release it and pressF
) (in the edit mode) shows a list of directories and files in the actual directory or will complete a directory or file name if the already typed prefix is unique. Of course, it works with absolute and relative paths too. - bookmark – to store/mark the actual position press
M
(lower case m) and then any letter key(in the command mode) as the mark of this bookmark, e.g.M A
for bookmark a orM Q
for bookmark q. To go to the bookmarked position, press`
and the stored mark, e.g.` A
will move you to the bookmark a.
If you need more a lot of bookmarks, you may use lower and upper case marks, i.e.M A
and` A
for mark a, butM Shift-A
and` Shift-A
for mark A. - comment out multiple lines – press
Ctrl-V
(the visual block) in the command mode and select more lines, then pressShift-I
to start the edit mode, insert one or more characters, e.g. press#
to comment out more lines in Ruby, and then pressEsc
(on some systems twice). (Motivated by Mark’s blog.)
To uncomment lines, pressCtrl-V
, select the block and pressd
to delete it.