vim-registers.md (4678B)
1 Title: Vim registers 2 Author: paco 3 Date: 2019-12-06 4 Type: article 5 6 ## TL;DR 7 8 Go to the end to see the [summary](#Summary) 9 10 ## Introducing Vim registers 11 12 Vim documentation says: 13 14 ``` 15 There are ten types of registers: 16 1. The unnamed register "" 17 2. 10 numbered registers "0 to "9 18 3. The small delete register "- 19 4. 26 named registers "a to "z or "A to "Z 20 5. Three read-only registers ":, "., "% 21 6. Alternate buffer register "# 22 7. The expression register "= 23 8. The selection registers "* and "+ 24 9. The black hole register "_ 25 10. Last search pattern register "/ 26 ``` 27 28 It may seem a lot, (and indeed the documentation for registers in Vim is a bit 29 complicated for my taste) and one may not use them all (at least not all the 30 time), but I'll try to describe them here in simple words. Of course I'll 31 leave a lot of functionality and corner cases out of this description. For 32 that, `:help registers`. 33 34 Remember that to interact with them you usually use `"` plus the name of the 35 register (ex: `"ad` deletes to register `"a`). On Vim scripts, you can refer 36 to them (for read and write if allowed) with `@` (ex: `:let @a = "foo"`) 37 38 You can see the contents of all the registers with `:registers` 39 40 ## The unnamed register 41 42 This is the one you're already using all the time, for deleting and yanking. 43 Everything goes here, no matter what it is, all the time (more or less, check 44 docs for details). That's why you get frustrated when you copy something and 45 then delete a character and override your copied text. 46 47 This is the register that is invoked if you do not specify a register. 48 49 So things like `d`, `c`, `x`, `dd`, `y` go into here, and things like `p` get 50 content from here. 51 52 ## The numbered registers 53 54 Those are of 2 types really: 55 56 * `"0` Any yank command goes here (and to `""` too) unless you specify another 57 registry. 58 * `"1` to `"9` Any delete or change command goes here unless you specify 59 another register or the change is "small" (less than one line). This works 60 like a queue, when you delete again, goes to `"1` and what was in `"1` goes 61 to `"2`, what was on `"2` to `"3` ... until what was on `"9` gets 62 discarded. So you have some sort of "delete history" 63 64 ## The small delete register 65 66 Here go all the delete operations less than one line long unless other register 67 specified. 68 69 ## Named registers 70 71 They are not automatically populated. You can use them explicitly to have 26 72 "clipboards" if you like. Keep in mind that they are shared with the macros. 73 When addressed upper-case, they append the content to the register instead of 74 replacing it. 75 76 Useful for reorganizing blocs too, you just append blocs of text to one 77 register in the order you want and then paste it in the place you want it to 78 be. 79 80 ## Read-only registers 81 82 * `".` Last inserted text. Just like the "dot" command. 83 * `"%` Name of the current file. 84 * `"` Most recent command line. 85 86 ## Alternate file register 87 88 Contains the name of the alternate file (`CTRL-^`), the one marked `#` on `:ls` 89 90 ## Expression register 91 92 This is not a register like the others. It allows you to execute an expression 93 that is converted to a string and put in place with `p` or automatically if you 94 are in insert mode and invoke it with `CTRL-R`. 95 96 ## Selection registers 97 98 Those are your system clipboards. Vim has to be compiled with support for 99 this. One is the primary and the other is the clipboard ([X11 stuff][1], you 100 know). I never remember which is which, so I have some aliases like this: 101 102 ``` 103 " copy and paste from system easily 104 if has("mac") 105 nnoremap cp "*p 106 xnoremap cy "*y 107 else 108 nnoremap cp "+p 109 xnoremap cy "+y 110 endif 111 ``` 112 113 ## Black hole register 114 115 This is like sending stuff to `/dev/null` 116 117 ## The last search pattern register 118 119 Contains the most recent search pattern. Not sure of its utility, but it's 120 there. 121 122 ## Summary 123 124 * If you do not specify a register, `""` (the unnamed register) is used. 125 * Your last copy is always available at `"0`. 126 * A "delete history" is available on `"1` to `"9`. 127 * Less than one line deletions are available on `"-` 128 * There are 26 "clipboards" in Vim (`"a` to `"z`) that can be appended 129 addressing them upper-case. 130 * You can evaluate expressions (like basic arithmetic and more) with the 131 expression register `"=` 132 * Interacting to and from the system clipboard is possible with the selection 133 registers. 134 * If you have something sensible that want to delete, use the black hole 135 register `"_` 136 * You can check the contents of all the registers with `:registers` 137 138 Hope it helps, and I hope I do not forget now. 139 140 [1]: https://unix.stackexchange.com/questions/139191/whats-the-difference-between-primary-selection-and-clipboard-buffer#139193