1. Vim Registers & Macros
A few mildly clever things that make vim the best
text editor around
2. The "default" register
You use one of them all the time
● The 'unnamed' register " is used for
every* delete, yank, change, substitute
and put
● AKA: The " register
* Except for those that are smaller than a line, but we'll fix that
3. Numbered Registers
A stack of previous yanks and deletes
● "0 is usually the same as ""
● Each yank or delete pushes a new "0 onto
the stack, thereby incrementing the "1
through "9
4. The only catch
"Small" deletes, namely those less than one
line in size, skip the numbered registers.
● I find this to be a pain in the ass
● :set clipboard=unnamed
○ This is the only change I'll ask you to make
○ This can run into a problem when running
vim inside tmux on OSX (see https://github.
com/ChrisJohnsen/tmux-MacOSX-
pasteboard)
○ This will replace your system clipboard's
contents
5. Letter registers
● Only used explicitly
● "fyy will yank the current line into the y
register
● "fp will paste that line
● These are also the registers used for
macros
6. The expression register
● "=
○ An embedded calculator
○ Usually accessed via Ctrl-r = when in insert
mode
7. The unloved rest
● "% Name of current file (Readonly)
● "# Name of alternate file (Readonly)
● ". Same text as the '.' command (Readonly)
● ": Text of last command-mode (Readonly)
● "* System clipboard - Only sometimes unloved
● "_ The black hole
● "/ Text of last search pattern
8. Macros
● When the '.' command isn't enough
● Can be a sign that code should be
refactored & simplified
● Useful on large swaths of copy needing
tedious and repetitive changes
● Can be useful in repetitive cucumber
features
9. Creating macros
● Start with q#{register}
● make appropriate changes
● end with q
-- OR --
● Put your sequence on a line and yank it
into the register
○ Prefer ^y$ over yy for this yank - avoid
extra ^J
10. Playing back macros
● @#{register} to replay macro stored in
register
● @@ to repeat last macro
● Can also prefix with a count
12. Best practices
● Recursion
○ Standard cautions
○ Start with qxqqx to clear register first
○ Once you have the base case working,
qX@xq will (usually) make it recursive
● Strive for single-line macros
○ Start with a search
○ End with j0 or j^
● :% normal @x is an alternative to
recursion
● Debug macros from registers