Normalize the registers

pull/65/head
Igor Irianto 3 years ago
parent 27d4886270
commit e29ac9bb15

@ -75,7 +75,7 @@ To see it in action, first you need to yank a word to register a. Move your curs
- `"a` tells Vim that the target of your next action will go to register a.
- `yiw` yanks inner word. Review the chapter on Vim grammar for a refresher.
Register "a now contains the word you just yanked. While in insert mode, to paste the text stored in register "a:
Register a now contains the word you just yanked. While in insert mode, to paste the text stored in register a:
```
Ctrl-R a

@ -41,13 +41,13 @@ p Paste the text after the cursor
P Paste the text before the cursor
```
Both `p` and `P` accept a count and a register symbol as arguments. For example, to paste ten times, do `10p`. To paste the text from register "a, do `"ap`. To paste the text from register "a ten times, do `10"ap`. By the way, the `p` actually technically stands for "put", not "paste", but I figure paste is a more conventional word.
Both `p` and `P` accept a count and a register symbol as arguments. For example, to paste ten times, do `10p`. To paste the text from register a, do `"ap`. To paste the text from register a ten times, do `10"ap`. By the way, the `p` actually technically stands for "put", not "paste", but I figure paste is a more conventional word.
The general syntax to get the content from a specific register is `"a`, where `a` is the register symbol.
## Calling Registers From Insert Mode
Everything you learn in this chapter can also be executed in insert mode. To get the text from register "a, normally you do `"ap`. But if you are in insert mode, run `Ctrl-R a`. The syntax to call registers from insert mode is:
Everything you learn in this chapter can also be executed in insert mode. To get the text from register a, normally you do `"ap`. But if you are in insert mode, run `Ctrl-R a`. The syntax to call registers from insert mode is:
```
Ctrl-R a
@ -136,13 +136,13 @@ Another example:
The named registers are Vim's most versatile register. It can store yanked, changed, and deleted texts into registers a-z. Unlike the previous 3 register types you've seen which automatically stores texts into registers, you have to explicitly tell Vim to use the named register, giving you full control.
To yank a word into register "a, you can do it with `"ayiw`.
- `"a` tells Vim that the next action (delete / change / yank) will be stored in register "a.
To yank a word into register a, you can do it with `"ayiw`.
- `"a` tells Vim that the next action (delete / change / yank) will be stored in register a.
- `yiw` yanks the word.
To get the text from register "a, run `"ap`. You can use all twenty-six alphabetical characters to store twenty-six different texts with named registers.
To get the text from register a, run `"ap`. You can use all twenty-six alphabetical characters to store twenty-six different texts with named registers.
Sometimes you may want to add to your existing named register. In this case, you can append your text instead of starting all over. To do that, you can use the uppercase version of that register. For example, suppose you have the word "Hello " already stored in register "a. If you want to add "world" into register "a, you can find the text "world" and yank it using "A register (`"Ayiw`).
Sometimes you may want to add to your existing named register. In this case, you can append your text instead of starting all over. To do that, you can use the uppercase version of that register. For example, suppose you have the word "Hello " already stored in register a. If you want to add "world" into register a, you can find the text "world" and yank it using A register (`"Ayiw`).
## The Read-Only Registers
@ -176,13 +176,13 @@ Here, you are telling Vim that you are using the expression register with `"=`.
Ctrl-R =1+1
```
You can also get the values from any register via the expression register when appended with `@`. If you wish to get the text from register "a:
You can also get the values from any register via the expression register when appended with `@`. If you wish to get the text from register a:
```
"=@a
```
Then press `<Enter>`, then `p`. Similarly, to get values from register "a while in insert mode:
Then press `<Enter>`, then `p`. Similarly, to get values from register a while in insert mode:
```
Ctrl-r =@a
@ -228,21 +228,21 @@ There is a plugin called [vim-peekaboo](https://github.com/junegunn/vim-peekaboo
The named registers are not just for storing texts. They can also execute macros with `@`. I will go over macros in the next chapter.
Keep in mind since macros are stored inside Vim registers, you can accidentally overwrite the stored text with macros. If you store the text "Hello Vim" in register "a and you later record a macro in the same register (`qa{macro-sequence}q`), that macro will overwrite your "Hello Vim" text stored earlier.
Keep in mind since macros are stored inside Vim registers, you can accidentally overwrite the stored text with macros. If you store the text "Hello Vim" in register a and you later record a macro in the same register (`qa{macro-sequence}q`), that macro will overwrite your "Hello Vim" text stored earlier.
## Clearing A Register
Technically, there is no need to clear any register because the next register you store under the same name will overwrite it. However, you can quickly clear any named register by recording an empty macro. For example, if you run `qaq`, Vim will record an empty macro in the register "a.
Technically, there is no need to clear any register because the next register you store under the same name will overwrite it. However, you can quickly clear any named register by recording an empty macro. For example, if you run `qaq`, Vim will record an empty macro in the register a.
Another alternative is to run the command `:call setreg('a', '')` where "a is the register "a.
Another alternative is to run the command `:call setreg('a', '')` where "a is the register a.
One more way to clear register is to set the content of "a register to an empty string with the expression `:let @a = ''`.
## Putting The Content Of A Register
You can use the `:put` command to paste the content of any one register. For example, if you run `:put a`, Vim will print the content of register "a below the current line. This behaves much like `"ap`, with the difference that the normal mode command `p` prints the register content after the cursor and the command `:put` prints the register content at newline.
You can use the `:put` command to paste the content of any one register. For example, if you run `:put a`, Vim will print the content of register a below the current line. This behaves much like `"ap`, with the difference that the normal mode command `p` prints the register content after the cursor and the command `:put` prints the register content at newline.
Since `:put` is a command-line command, you can pass it an address. `:10put a` will paste text from register "a to below line 10.
Since `:put` is a command-line command, you can pass it an address. `:10put a` will paste text from register a to below line 10.
One cool trick to pass `:put` with the black hole register (`"_`). Since the black hole register does not store any text, `:put _` will insert a blank line instead. You can combine this with the global command to insert multiple blank lines. For example, to insert blank lines below all lines that contain the text "end", run `:g/end/put _`. You will learn about the global command later.
@ -257,6 +257,6 @@ I don't think you should memorize all the registers immediately. To become produ
Since the unnamed register defaults to `p` and `P`, you only have to learn two registers: the named registers and the numbered registers. Gradually learn more registers when you need them. Take your time.
The average human has a limited short-term memory capacity, about 5 - 7 items at once. That is why in my everyday editing, I only use about 5 - 7 named registers. There is no way I can remember all twenty-six in my head. I normally start with register "a, then "b, ascending the alphabetical order. Try it and experiment around to see what technique works best for you.
The average human has a limited short-term memory capacity, about 5 - 7 items at once. That is why in my everyday editing, I only use about 5 - 7 named registers. There is no way I can remember all twenty-six in my head. I normally start with register a, then b, ascending the alphabetical order. Try it and experiment around to see what technique works best for you.
Vim registers are powerful. Used strategically, it can save you from typing countless repeating texts. Next, let's learn about macros.

@ -82,7 +82,7 @@ The `:normal` command accepts range as arguments. You can use this to run macro
## Executing A Macro Across Multiple Files
Suppose you have multiple `.txt` files, each contains some texts. Your task is to uppercase the first word only on lines containing the word "donut". Assume you have `0W~j` in register "a (the same macro as before). How can you quickly accomplish this?
Suppose you have multiple `.txt` files, each contains some texts. Your task is to uppercase the first word only on lines containing the word "donut". Assume you have `0W~j` in register a (the same macro as before). How can you quickly accomplish this?
First file:
@ -136,7 +136,7 @@ qaqqa0W~j@aq
Here is the breakdown of the steps:
- `qaq` records an empty macro "a. It is necessary to start with an empty register because when you recursively call the macro, it will run whatever is in that register.
- `qa` starts recording on register "a.
- `qa` starts recording on register a.
- `0` goes to the first character in the current line.
- `W` goes to the next WORD.
- `~` toggles the case of the character under the cursor.
@ -150,9 +150,9 @@ How did the macro know when to stop? When the macro was on the last line, it tri
## Appending A Macro
If you need to add actions to an existing macro, instead of recreating the macro from scratch, you can append actions to an existing one. In the register chapter, you learned that you can append a named register by using its uppercased symbol. The same rule applies. To append actions to register "a macro, use register "A.
If you need to add actions to an existing macro, instead of recreating the macro from scratch, you can append actions to an existing one. In the register chapter, you learned that you can append a named register by using its uppercased symbol. The same rule applies. To append actions to register a macro, use register "A.
In addition, you also want to add a dot at the end of the line. Assume you have the actions `0W~` stored in register "a already (it's a little different from the previous macro, it omits the `j` motion), run:
In addition, you also want to add a dot at the end of the line. Assume you have the actions `0W~` stored in register a already (it's a little different from the previous macro, it omits the `j` motion), run:
```
qAA.<Esc>q
@ -179,7 +179,7 @@ c. powdered sugar donut
d. plain donut
```
First, let's call the existing macro (assume you have kept the macro from the previous section in register "a) with `:put a`:
First, let's call the existing macro (assume you have kept the macro from the previous section in register a) with `:put a`:
```
0W~A.^[
@ -201,15 +201,15 @@ There is a small problem. Vim does not understand `<Esc>`. You can't literally t
0W~$bideep fried ^[A.^[
```
To add the amended instruction into register "a, you can do it the same way as adding a new entry into a named register. At the start of the line, run `"ay$` to store the yanked text in register "a.
To add the amended instruction into register a, you can do it the same way as adding a new entry into a named register. At the start of the line, run `"ay$` to store the yanked text in register a.
Now when you execute `@a`, your macro will toggle the case of the first word, add "deep fried " before "donut", and add a "." at the end of the line. Yum!
An alternative way to amend a macro is to use a command line expression. Do `:let @a="`, then do `Ctrl-R Ctrl-R a`, this will literally paste the content of register "a. Finally, don't forget to close the double quotes (`"`). You might have something like `:let @a="0W~$bideep fried ^[A.^["`.
An alternative way to amend a macro is to use a command line expression. Do `:let @a="`, then do `Ctrl-R Ctrl-R a`, this will literally paste the content of register a. Finally, don't forget to close the double quotes (`"`). You might have something like `:let @a="0W~$bideep fried ^[A.^["`.
## Macro Redundancy
You can easily duplicate macros from one register to another. For example, to duplicate a macro in register "a to register "z, you can do `:let @z = @a`. `@a` represents the content of register "a. Now if you run `@z`, it does the exact same actions as `@a`.
You can easily duplicate macros from one register to another. For example, to duplicate a macro in register a to register z, you can do `:let @z = @a`. `@a` represents the content of register a. Now if you run `@z`, it does the exact same actions as `@a`.
I find creating a redundancy useful on my most frequently used macros. In my workflow, I usually record macros in the first seven alphabetical letters (a-g) and I often replace them without much thought. If I move the useful macros towards the end of the alphabets, I can preserve them without worrying that I might accidentally replace them.
@ -232,7 +232,7 @@ qa0f{gui{jq
```
The breakdown:
- `qa` starts recording in register "a.
- `qa` starts recording in register a.
- `0` goes to first line.
- `f{` finds the first instance of "{".
- `gui{` lowercases (`gu`) the text inside the bracket text-object (`i{`).

@ -189,7 +189,7 @@ const three = 3
console.log("three: ", three);
```
Notice that the lines with "const" do not have semi-colons. Let's create a macro to add a comma to the end of those lines in the register "a:
Notice that the lines with "const" do not have semi-colons. Let's create a macro to add a comma to the end of those lines in the register a:
```
qa0A;<Esc>q

@ -46,7 +46,7 @@ This also allows you to search through the previous commands, edit them and reru
## Register And Autocomplete
While in the command-line mode, you can insert texts from Vim register with `Ctrl-R` the same way as the insert mode. If you have the string "foo" saved in the register "a", you can insert it by running `Ctrl-R a`. Everything that you can get from the register in the insert mode, you can do the same from the command-line mode.
While in the command-line mode, you can insert texts from Vim register with `Ctrl-R` the same way as the insert mode. If you have the string "foo" saved in the register a, you can insert it by running `Ctrl-R a`. Everything that you can get from the register in the insert mode, you can do the same from the command-line mode.
In addition, you can also get the word under the cursor with `Ctrl-R Ctrl-W` (`Ctrl-R Ctrl-A` for the WORD under cursor). To get the line under the cursor, use `Ctrl-R Ctrl-L`. To get the filename under the cursor, use `Ctrl-R Ctrl-F`.

@ -291,7 +291,7 @@ Session is a useful tool to preserve your project's external attributes. However
## Viminfo
If you notice, after yanking a word into register "a and quitting Vim, the next time you open Vim you still that text stored in the register. This is actually a work of Viminfo. Without it, Vim won't remember the register after you close Vim.
If you notice, after yanking a word into register a and quitting Vim, the next time you open Vim you still that text stored in the register. This is actually a work of Viminfo. Without it, Vim won't remember the register after you close Vim.
If you use Vim 8 or higher, Vim enables Viminfo by default, so you may have been using Viminfo this whole time without knowing it!

Loading…
Cancel
Save