Vim's : (Colon) Register

by Noah Frederick | | level: intermediate

In addition to the named registers a-z and the numbered registers 0-9, Vim has quite a few others including the read-only command register :. Executing a command in command-line mode, such as :write, populates the colon register. You can verify this with the :reg[isters] command.

Persisting an interactively set option

Let’s imagine you turned on line numbers with :set number, and you want to make the change persist across sessions. In your .vimrc file, you can do a simple ":p to put (paste) the contents of the register. The value in this becomes more evident when you consider doing the same thing with, e.g., a complex :s[ubstitute] pattern that you constructed interactively and now want to save. Developing custom mappings and commands interactively is handy, and the colon register is an easy way of getting them into your buffer.

Repeating the last command-line mode command

If you use macros, you are familiar with the @x normal mode command, which plays back the key presses stored in register x. Thus, to repeat the last command-line mode command, use @:. Note that : contains the last command that you typed manually—commands executed from a script or mapping do not change the register’s contents.

Further reading