Wiki: VimL

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. vimtutor fr

Normal mode

Normal vim commands can be entered with the norm command. For example, to insert the numbers 1 through 10, you might write:

norm10O0<Esc>V(g<C-a>
x

Where <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 [^A-Za-z]).

Macros

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:

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:

Command Mode

Variables

Special Variable _

You can use _ as a variable name to save space around if, let, for, etc...

for n in range(10)
pu=n
endfo
x

becomes

for_ in range(10)
pu=_
endfo
x

Registers

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

Note that += and -= 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.

Links

Udioica on golfing macros:

Udioica's blog explains in details some of his vimgolf solutions: http://udioica.blogspot.com/