mirror of
https://github.com/iggredible/Learn-Vim
synced 2024-11-26 20:40:18 +00:00
Normalize the syntax for registers
This commit is contained in:
parent
9987438ea4
commit
3d9c5dc425
6 changed files with 34 additions and 34 deletions
|
@ -75,7 +75,7 @@ To see it in action, first you need to yank a word to register a. You can do thi
|
|||
- `"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.
|
||||
|
||||
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
|
||||
|
|
|
@ -44,13 +44,13 @@ p Put the text after the cursor
|
|||
P Put the text before the cursor
|
||||
```
|
||||
|
||||
Both `p` and `P` accept a count and a register symbol as arguments. For example, to put the recently yanked text ten times, do `10p`. To put the text from register "a", do `"ap`. To put the text from register "a" ten times, do `10"ap`.
|
||||
Both `p` and `P` accept a count and a register symbol as arguments. For example, to put the recently yanked text ten times, do `10p`. To put the text from register "a, do `"ap`. To put the text from register "a ten times, do `10"ap`.
|
||||
|
||||
The general syntax to get the content from a specific register is `"x`, where `x` 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 x
|
||||
```
|
||||
|
@ -137,13 +137,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,12 +176,12 @@ Here, you are telling Vim that you are using the expression register with `"=`.
|
|||
Ctrl-r =1+1
|
||||
```
|
||||
|
||||
You can get the values from any register using the expression register with `@`. If you wish to get the text from register "a":
|
||||
You can get the values from any register using the expression register 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
|
||||
|
@ -227,21 +227,21 @@ To paste your last search (`/` or `?`) query, you can use the last search patter
|
|||
|
||||
## Viewing The Registers
|
||||
|
||||
To view all your registers, use the `:register` command. To view only registers "a", "1", and "-", use `:register a 1 -`.
|
||||
To view all your registers, use the `:register` command. To view only registers "a, "1, and "-, use `:register a 1 -`.
|
||||
|
||||
There is a plugin called [vim-peekaboo](https://github.com/junegunn/vim-peekaboo) that lets you to peek into the contents of the registers when you hit `"` or `@` in normal mode and `Ctrl-r` in insert mode. I find this plugin very useful because most times, I can't remember the content in my registers. Give it a try!
|
||||
|
||||
## Executing A Register
|
||||
|
||||
The named registers are not just for storing texts. They can also be used to execute macros with `@`. I will go over macros in the next chapter. If you store the text "Hello Vim" in register "a", and you later record a macro in the same register (`qa{macro-commands}q`), that macro will overwrite your "Hello Vim" text stored earlier (you can execute the macro stored in register "a" with `@a`).
|
||||
The named registers are not just for storing texts. They can also be used to execute macros with `@`. I will go over macros in the next chapter. If you store the text "Hello Vim" in register "a, and you later record a macro in the same register (`qa{macro-commands}q`), that macro will overwrite your "Hello Vim" text stored earlier (you can execute the macro stored in register "a with `@a`).
|
||||
|
||||
## 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". 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 = ''`.
|
||||
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. 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". 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. 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.
|
||||
|
||||
TODO:
|
||||
One cool trick is t quickly add a null register (`_`) to insert a blank line. `:put _` adds a blank line. You can combine this with the global command (will cover global command later), such as `:g/^$/put _` to put a blank line after a blank line.
|
||||
|
@ -257,6 +257,6 @@ I don't think you should memorize everything right away. To become productive, y
|
|||
|
||||
Since the unnamed register defaults to `p` or `P`, you only have to learn two registers: the named registers and the numbered registers. Gradually learn more when you need it. Take your time.
|
||||
|
||||
The average human has a limited short-term memory capacity, about seven items at once. That is why in my everyday editing, I only use about three to seven 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 seven items at once. That is why in my everyday editing, I only use about three to seven 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 repeated texts. But now, it's time to learn about macros.
|
||||
|
|
|
@ -41,13 +41,13 @@ qa0gU$jq
|
|||
|
||||
Here is the breakdown of the command above:
|
||||
|
||||
- `qa` starts recording a macro in the "a" register.
|
||||
- `qa` starts recording a macro in the "a register.
|
||||
- `0` goes to beginning of the line.
|
||||
- `gU$` uppercases the text from your current location to the end of the line.
|
||||
- `j` goes down one line.
|
||||
- `q` stops recording.
|
||||
|
||||
To replay that macro, run `@a`. Just like many other Vim commands, you can pass a count argument to macros. For example, you can run `3@a` to execute "a" macro three times. You can run `3@@` to execute the last executed macro three times.
|
||||
To replay that macro, run `@a`. Just like many other Vim commands, you can pass a count argument to macros. For example, you can run `3@a` to execute "a macro three times. You can run `3@@` to execute the last executed macro three times.
|
||||
|
||||
## Safety Guard
|
||||
|
||||
|
@ -67,7 +67,7 @@ qa0W~jq
|
|||
```
|
||||
|
||||
Here's the breakdown of the command above:
|
||||
- `qa` starts recording a macro in the "a" register.
|
||||
- `qa` starts recording a macro in the "a register.
|
||||
- `0` goes to the beginning of the line.
|
||||
- `W` goes to the next WORD.
|
||||
- `~` toggles the case of the character under the cursor.
|
||||
|
@ -83,7 +83,7 @@ The fact that macro execution stops upon the first error encounter is a good fea
|
|||
|
||||
Running `@a` in normal mode is not the only way you can execute macros in Vim. You can also run `:normal @a` command line. `:normal` allows the user to execute any normal mode command given as argument. By passing it `@a`, it is the same as running `@a` from normal mode.
|
||||
|
||||
The `:normal` command accepts range as arguments. You can use this to run macro in select ranges. If you want to execute your "a" macro between lines 2 and 3, you can run `:2,3 normal @a`. I will go over command line commands in a later chapter.
|
||||
The `:normal` command accepts range as arguments. You can use this to run macro in select ranges. If you want to execute your "a macro between lines 2 and 3, you can run `:2,3 normal @a`. I will go over command line commands in a later chapter.
|
||||
|
||||
## Executing A Macro Across Multiple Files
|
||||
|
||||
|
@ -135,13 +135,13 @@ qaqqa0W~j@aq
|
|||
```
|
||||
|
||||
Here is the breakdown of the steps:
|
||||
- `qaq` records an empty macro "a". It is necessary to record an empty macro in the same register name because when you execute the "a" macro later, you don't want that register to contain anything.
|
||||
- `qa` starts recording on register "a".
|
||||
- `qaq` records an empty macro "a. It is necessary to record an empty macro in the same register name because when you execute the "a macro later, you don't want that register to contain anything.
|
||||
- `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.
|
||||
- `j` goes down one line.
|
||||
- `@a` executes macro "a". When recording this, `@a` should be empty because you had just called `qaq`.
|
||||
- `@a` executes macro "a. When recording this, `@a` should be empty because you had just called `qaq`.
|
||||
- `q` stops recording.
|
||||
|
||||
Now you can just run `@a` and watch Vim execute the macro recursively.
|
||||
|
@ -150,9 +150,9 @@ How does the macro know when to stop? When the macro is on the last line, it tri
|
|||
|
||||
## Appending A Macro
|
||||
|
||||
If you need to add more actions to an existing macro, instead of redoing it, you can append actions to it. In the register chapter, you learned that you can append a named register by using its uppercased symbol. To append actions to a macro in register "a", use register "A". Suppose in addition to toggling the case of the first word, you also want to add a dot at the end of the line.
|
||||
If you need to add more actions to an existing macro, instead of redoing it, you can append actions to it. In the register chapter, you learned that you can append a named register by using its uppercased symbol. To append actions to a macro in register "a, use register "A. Suppose in addition to toggling the case of the first word, you also want to add a dot at the end of the line.
|
||||
|
||||
Assume you have the following actions stored as a macro in register "a":
|
||||
Assume you have the following actions stored as a macro in register "a:
|
||||
|
||||
```
|
||||
0W~
|
||||
|
@ -162,7 +162,7 @@ This is how you can do it:
|
|||
qAA.<esc>q
|
||||
```
|
||||
The breakdown:
|
||||
- `qA` starts recording the macro in register "A".
|
||||
- `qA` starts recording the macro in register "A.
|
||||
- `A.<esc>` inserts a dot (".") at the end of the line (`A`), then exits insert mode (`<esc>`).
|
||||
- `q` stops recording macro.
|
||||
|
||||
|
@ -182,7 +182,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.^[
|
||||
|
@ -204,15 +204,15 @@ There is a small problem. Vim does not understand `<esc>`. You will have to writ
|
|||
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$`. This tells Vim that you're using the named register "a" (`"a`) to store the yanked text from the current position to the end of the line (`y$`).
|
||||
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$`. This tells Vim that you're using the named register "a (`"a`) to store the yanked text from the current position to the end of the line (`y$`).
|
||||
|
||||
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.
|
||||
|
||||
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 (`"`). If you need to insert special characters using internal codes while editing a command line expression, you can use `Ctrl-v`.
|
||||
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 (`"`). If you need to insert special characters using internal codes while editing a command line expression, you can use `Ctrl-v`.
|
||||
|
||||
## 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.
|
||||
|
||||
|
@ -235,7 +235,7 @@ qa0f{gui{jq
|
|||
```
|
||||
|
||||
Here is 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{`).
|
||||
|
@ -255,7 +255,7 @@ import { FUNC5 } from "library5";
|
|||
|
||||
Running `99@a`, only executes the macro three times. It does not execute the macro on last two lines because the execution fails to run `f{` on the "foo" line. This is expected when running the macro in series. You can always go to the next line where "FUNC4" is and replay that macro again. But what if you want to get everything done in one go? You can run the macro in parallel.
|
||||
|
||||
Recall from earlier section that macros can be executed using the command line command `:normal` (ex: `:3,5 normal @a` to execute macro "a" on lines 3-5). If you run `:1,$ normal @a`, you will see that the macro is being executed on all lines except the "foo" line. It works!
|
||||
Recall from earlier section that macros can be executed using the command line command `:normal` (ex: `:3,5 normal @a` to execute macro "a on lines 3-5). If you run `:1,$ normal @a`, you will see that the macro is being executed on all lines except the "foo" line. It works!
|
||||
|
||||
Although internally Vim does not actually run the macros in parallel, outwardly, it behaves like such. Vim executes `@a` *independently* on each line from the first line to the last line (`1,$`). Since Vim executes these macros independently, each line does not know that one of the macro executions had failed on the "foo" line.
|
||||
|
||||
|
@ -265,4 +265,4 @@ Many things you do in editing are repetitive. To get better at editing, get into
|
|||
|
||||
In the beginning, I find it very awkward to write macros, but don't give up. With enough practice, you will get into the habit of automating everything.
|
||||
|
||||
You might find it helpful to use mnemonics to help remember your macros. If you have a macro that creates a function, use the "f" register (`qf`). If you have a macro for numerical operations, the "n" register may be a good fit (`qn`). Name it with the *first named register* that comes to your mind when you think of that operation. I also find that "q" register makes a good default macro register because `qq` does not require much brain power to use. Lastly, I like to increment my macros in alphabetical orders, like `qa`, then `qb`, then `qc`, and so on. Find a method that works best for you.
|
||||
You might find it helpful to use mnemonics to help remember your macros. If you have a macro that creates a function, use the "f register (`qf`). If you have a macro for numerical operations, the "n register may be a good fit (`qn`). Name it with the *first named register* that comes to your mind when you think of that operation. I also find that "q register makes a good default macro register because `qq` does not require much brain power to use. Lastly, I like to increment my macros in alphabetical orders, like `qa`, then `qb`, then `qc`, and so on. Find a method that works best for you.
|
||||
|
|
|
@ -301,7 +301,7 @@ In a regular editor, after you highlight a text block and type a letter, say the
|
|||
|
||||
If you highlight a line of text with line-wise select mode (`gH`) and type "y", it will delete the highlighted text and insert the letter "y", much like the regular text editor.
|
||||
|
||||
Contrast this behavior with visual mode: if you highlight a line of text with line-wise visual mode (`V`) and type "y", the highlighted text will not be deleted and replaced by the literal letter "y". It will only be yanked and stored in the yanked register `"0`.
|
||||
Contrast this behavior with visual mode: if you highlight a line of text with line-wise visual mode (`V`) and type "y", the highlighted text will not be deleted and replaced by the literal letter "y". It will only be yanked and stored in the yanked register "0.
|
||||
|
||||
I personally never used select mode, but it's good to know that it exists.
|
||||
|
||||
|
|
|
@ -193,7 +193,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
|
||||
|
|
|
@ -52,7 +52,7 @@ This also allows you to search through the previous commands, edit them and reru
|
|||
|
||||
When programming, whenever possible, do not repeat if you can autocomplete it. This mindset will not only save you time but reduces the chances of typing the wrong characters.
|
||||
|
||||
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" (`"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.
|
||||
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.
|
||||
|
||||
You can also autocomplete commands. To autocomplete the `echo` command, while in the command-line mode, type "ec", then press `<Tab>`. You should see on the bottom left Vim commands starting with "ec" (example: `echo echoerr echohl echomsg econ`). To go to the next option, press either `<Tab>` or `Ctrl-n`. To go the previous option, press either `<Shift-Tab>` or `Ctrl-p`.
|
||||
|
||||
|
|
Loading…
Reference in a new issue