Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Vim Registers & MacrosA few mildly clever things that make vim the best                                text editor around
The "default" registerYou use one of them all the time● The unnamed register " is used for  every* delete, yank, change, s...
Numbered RegistersA stack of previous yanks and deletes● "0 is usually the same as ""● Each yank or delete pushes a new "0...
The only catch"Small" deletes, namely those less than oneline in size, skip the numbered registers.● I find this to be a p...
Letter registers● Only used explicitly● "fyy will yank the current line into the y  register● "fp will paste that line● Th...
The expression register● "=  ○ An embedded calculator  ○ Usually accessed via Ctrl-r = when in insert    mode
The unloved rest●   "% Name of current file (Readonly)●   "# Name of alternate file (Readonly)●   ". Same text as the . co...
Macros● When the . command isnt enough● Can be a sign that code should be  refactored & simplified● Useful on large swaths...
Creating macros●    Start with q#{register}●    make appropriate changes●    end with q--   OR --●    Put your sequence on...
Playing back macros● @#{register} to replay macro stored in  register● @@ to repeat last macro● Can also prefix with a count
Examples● <p><b>Something</b></p> into  <h4>Something</h4>● Reorder method parameters● Change YOB to current age
Best practices● Recursion  ○ Standard cautions  ○ Start with qxqqx to clear register first  ○ Once you have the base case ...
Questions?● Paul Ostazeski, Developer at SmartLogic  Solutions● @paulostazeski
Upcoming SlideShare
Loading in …5
×

Vim Registers & Macros

739 views

Published on

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

Published in: Technology, Business
  • Be the first to comment

Vim Registers & Macros

  1. 1. Vim Registers & MacrosA few mildly clever things that make vim the best text editor around
  2. 2. The "default" registerYou 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 well fix that
  3. 3. Numbered RegistersA 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. 4. The only catch"Small" deletes, namely those less than oneline in size, skip the numbered registers.● I find this to be a pain in the ass● :set clipboard=unnamed ○ This is the only change Ill 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 clipboards contents
  5. 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. 6. The expression register● "= ○ An embedded calculator ○ Usually accessed via Ctrl-r = when in insert mode
  7. 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. 8. Macros● When the . command isnt 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. 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. 10. Playing back macros● @#{register} to replay macro stored in register● @@ to repeat last macro● Can also prefix with a count
  11. 11. Examples● <p><b>Something</b></p> into <h4>Something</h4>● Reorder method parameters● Change YOB to current age
  12. 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
  13. 13. Questions?● Paul Ostazeski, Developer at SmartLogic Solutions● @paulostazeski

×