For people completely new to vim check out the
vimtutor command. It is a 30 minutes hands-on tutorial. It should be available in a terminal if you have vim installed. Also available in other languages e.g.
Normal vim commands can be entered with the
norm command. For example, to insert the numbers 1 through 10, you might write:
<Esc> is a literal escape char (0x1b), and
<C-a> is a literal Ctrl-A (0x01).
Note that a space is not required between a command name and a digit (or any character
Unfortunately, macros cannot be stored in registers using
q as one would expect (the register is instead cleared (after being created if necessary)). However, a number of auto-registers can be used for the same purpose, including:
@"- Last delete or yank
@0- Last yank
@3, etc. - Last "long" delete (full line or more), second to last delete, third to last delete, etc.
@-- Last "short" delete (less than a full line)
@.- Last inserted text (specifically, in normal mode with one of
For example, to copy a line, paste it below and increment 10 times, you might write this:
i 0Yp<C-a> . norm lD10@- x
A few things to note:
- Full line yanks and deletes will contain a trailing newline, that will be executed as part of the macro.
- Control characters inserted in command mode (as above) do not require escaping (with
<C-v>), but do when inserted in normal mode.
- It is also possible to yank/delete directly into a register, e.g.
You can use
_ as a variable name to save space around
for n in range(10) pu=n endfo x
for_ in range(10) pu=_ endfo x
Registers can also be used as variables, which can save bytes:
for@0in range(10) pu endfo x
Registers can also be used without being explicitly declared:
for_ in range(10) let@0.=_ endfo pu! x
-= are not allowed with registers, as they are implicitly strings.
Useful for code.golf:
pu!=args puts all arguments on a new line and leaves a blank line at the end, which does not break the diff.
Udioica on golfing macros:
Udioica's blog explains in details some of his vimgolf solutions: http://udioica.blogspot.com/