13

Click here to load reader

Vim Registers & Macros

Embed Size (px)

DESCRIPTION

A few mildly clever things that make Vim the best text editor around, from SmartLogic's Paul Ostazeski.

Citation preview

Page 1: Vim Registers & Macros

Vim Registers & MacrosA few mildly clever things that make vim the best

text editor around

Page 2: Vim Registers & Macros

You use one of them all the time● The 'unnamed' register " is used for

every* delete, yank, change, substitute and put

● AKA: The " register

The "default" register

* Except for those that are smaller than a line, but we'll fix that

Page 3: Vim Registers & Macros

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

Numbered Registers

Page 4: Vim Registers & Macros

"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

The only catch

Page 5: Vim Registers & Macros

● 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

Letter registers

Page 6: Vim Registers & Macros

● "=○ An embedded calculator○ Usually accessed via Ctrl-r = when in insert

mode

The expression register

Page 7: Vim Registers & Macros

● "% 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

The unloved rest

Page 8: Vim Registers & 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

Macros

Page 9: Vim Registers & 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

Creating macros

Page 10: Vim Registers & Macros

● @#{register} to replay macro stored in register

● @@ to repeat last macro● Can also prefix with a count

Playing back macros

Page 11: Vim Registers & Macros

● <p><b>Something</b></p> into <h4>Something</h4>

● Reorder method parameters● Change YOB to current age

Examples

Page 12: Vim Registers & Macros

● 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

Best practices