Click here to load reader
Upload
smartlogic
View
469
Download
2
Embed Size (px)
DESCRIPTION
A few mildly clever things that make Vim the best text editor around, from SmartLogic's Paul Ostazeski.
Citation preview
Vim Registers & MacrosA few mildly clever things that make vim the best
text editor around
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
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
"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
● 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
● "=○ An embedded calculator○ Usually accessed via Ctrl-r = when in insert
mode
The expression register
● "% 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
● 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
● 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
● @#{register} to replay macro stored in register
● @@ to repeat last macro● Can also prefix with a count
Playing back macros
● <p><b>Something</b></p> into <h4>Something</h4>
● Reorder method parameters● Change YOB to current age
Examples
● 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
● Paul Ostazeski, Developer at SmartLogic Solutions
● @paulostazeski
Questions?