Learn-Vim/ch07_the_dot_command.md

120 lines
5.9 KiB
Markdown
Raw Permalink Normal View History

2021-05-29 15:17:31 +00:00
# Ch07. the Dot Command
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
In general, you should try to avoid redoing what you just did whenever possible. In this chapter, you will learn how to use the dot command to easily redo the previous change. It is a versatile command for reducing simple repetitions.
2020-08-07 15:25:40 +00:00
2020-11-13 01:02:04 +00:00
## Usage
2020-08-07 15:25:40 +00:00
2020-12-07 14:21:01 +00:00
Just like its name, you can use the dot command by pressing the dot key (`.`).
2020-08-07 15:25:40 +00:00
For example, if you want to replace all "let" with "const" in the following expressions:
2021-01-20 17:24:32 +00:00
2020-08-07 15:25:40 +00:00
```
let one = "1";
let two = "2";
let three = "3";
```
2021-01-20 17:24:32 +00:00
- Search with `/let` to go to the match.
2021-04-17 13:57:08 +00:00
- Change with `cwconst<Esc>` to replace "let" with "const".
2021-01-20 17:24:32 +00:00
- Navigate with `n` to find the next match using the previous search.
- Repeat what you just did with the dot command (`.`).
- Continue pressing `n . n .` until you replace every word.
2020-08-07 15:25:40 +00:00
2021-01-28 16:32:18 +00:00
Here the dot command repeated the `cwconst<Esc>` sequence. It saved you from typing eight keystrokes in exchange for just one.
2020-08-07 15:25:40 +00:00
2021-05-29 15:17:31 +00:00
## What Is a Change?
2020-08-07 15:25:40 +00:00
If you look at the definition of the dot command (`:h .`), it says that the dot command repeats the last change. What is a change?
2021-01-20 17:24:32 +00:00
Any time you update (add, modify, or delete) the content of the current buffer, you are making a change. The exceptions are updates done by command-line commands (the commands starting with `:`) do not count as a change.
2020-08-07 15:25:40 +00:00
2021-01-28 16:32:18 +00:00
In the first example, `cwconst<Esc>` was the change. Now suppose you have this text:
2020-08-07 15:25:40 +00:00
```
pancake, potatoes, fruit-juice,
```
2021-01-20 17:24:32 +00:00
To delete the text from the start of the line to the next occurrence of a comma, first delete to the comma, then repeat twice it with `df,..`.
2020-08-07 15:25:40 +00:00
Let's try another example:
2021-01-20 17:24:32 +00:00
2020-08-07 15:25:40 +00:00
```
pancake, potatoes, fruit-juice,
```
2021-05-02 18:39:18 +00:00
This time, your task is to delete the comma, not the breakfast items. With the cursor at the beginning of the line, go to the first comma, delete it, then repeat two more times with `f,x..` Easy, right? Wait a minute, it didn't work! Why?
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
A change excludes motions because it does not update buffer content. The command `f,x` consisted of two actions: the command `f,` to move the cursor to "," and `x` to delete a character. Only the latter, `x`, caused a change. Contrast that with `df,` from the earlier example. In it, `f,` is a directive to the delete operator `d`, not a motion to move the cursor. The `f,` in `df,` and `f,x` have two very different roles.
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
Let's finish the last task. After you run `f,` then `x`, go to the next comma with `;` to repeat the latest `f`. Finally, use `.` to delete the character under the cursor. Repeat `; . ; .` until everything is deleted. The full command is `f,x;.;.`.
2020-08-07 15:25:40 +00:00
Let's try another one:
```
pancake
potatoes
fruit-juice
```
Let's add a comma at the end of each line. Starting at the first line, do `A,<Esc>j`. By now, you realize that `j` does not cause a change. The change here is only `A,`. You can move and repeat the change with `j . j .`. The full command is `A,<Esc>j.j.`.
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
Every action from the moment you press the insert command operator (`A`) until you exit the insert command (`<Esc>`) is considered as a change.
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
## Multi-line Repeat
2020-08-07 15:25:40 +00:00
Suppose you have this text:
2021-01-20 17:24:32 +00:00
2020-08-07 15:25:40 +00:00
```
let one = "1";
let two = "2";
let three = "3";
const foo = "bar';
let four = "4";
let five = "5";
let six = "6";
let seven = "7";
let eight = "8";
let nine = "9";
```
2021-01-20 17:24:32 +00:00
Your goal is to delete all lines except the "foo" line. First, delete the first three lines with `d2j`, then to the line below the "foo" line. On the next line, use the dot command twice. The full command is `d2jj..`.
Here the change was `d2j`. In this context, `2j` was not a motion, but a part of the delete operator.
2020-08-07 15:25:40 +00:00
Let's look at another example:
2021-01-20 17:24:32 +00:00
2020-08-07 15:25:40 +00:00
```
zlet zzone = "1";
zlet zztwo = "2";
zlet zzthree = "3";
let four = "4";
```
2021-10-17 01:30:31 +00:00
Let's remove all the z's. Starting from the first character on the first line, visually select only the first z from the first three lines with blockwise visual mode (`Ctrl-Vjj`). If you're not familiar with blockwise visual mode, I will cover them in a later chapter. Once you have the three z's visually selected, delete them with the delete operator (`d`). Then move to the next word (`w`) to the next z. Repeat the change two more times (`..`). The full command is `Ctrl-vjjdw..`.
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
When you deleted a column of three z's (`Ctrl-vjjd`), it was counted as a change. Visual mode operation can be used to target multiple lines as part of a change.
2020-08-07 15:25:40 +00:00
2021-05-29 15:17:31 +00:00
## Including a Motion in a Change
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
Let's revisit the first example in this chapter. Recall that the command `/letcwconst<Esc>` followed by `n . n .` replaced all "let" with "const" in the following expressions:
2020-08-07 15:25:40 +00:00
```
let one = "1";
let two = "2";
let three = "3";
```
2021-01-20 17:24:32 +00:00
There is a faster way to accomplish this. After you searched `/let`, run `cgnconst<Esc>` then `. .`.
`gn` is a motion that searches forward for the last search pattern (in this case, `/let`) and automatically does a visual highlight. To replace the next occurrence, you no longer have to move and repeat the change ( `n . n .`), but only repeat (`. .`). You do not have to use search motions anymore because searching the next match is now part of the change!
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
When you are editing, always be on the lookout for motions that can do several things at once like `gn` whenever possible.
2020-08-07 15:25:40 +00:00
2021-05-29 15:17:31 +00:00
## Learn the Dot Command the Smart Way
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
The dot command's power comes from exchanging several keystrokes for one. It is probably not a profitable exchange to use the dot command for single key operations like `x`. If your last change requires a complex operation like `cgnconst<Esc>`, the dot command reduces nine keypresses into one, a very profitable trade-off.
2020-08-07 15:25:40 +00:00
2021-01-20 17:24:32 +00:00
When editing, think about repeatability. For example, if I need to remove the next three words, is it more economical to use `d3w` or to do `dw` then `.` two times? Will you be deleting a word again? If so, then it makes sense to use `dw` and repeat it several times instead of `d3w` because `dw` is more reusable than `d3w`.
2020-08-07 15:25:40 +00:00
2022-01-24 16:03:20 +00:00
The dot command is a versatile command for automating single changes. In a later chapter, you will learn how to automate more complex actions with Vim macros. But first, let's learn about registers to store and retrieve text.