Update the wording on ch13 to explain better

pull/59/head
Igor Irianto 3 years ago
parent 8bc091ce56
commit f672da7cf0

@ -396,7 +396,7 @@ By passing `_` after `d`, Vim won't save the deleted lines into any registers.
## Reduce Multiple Empty Lines To One Empty Line
If you have a file with multiple empty lines like the following:
If you have a text with multiple empty lines:
```
const one = 1;
@ -414,7 +414,7 @@ const three = 3;
console.log("three: ", three);
```
You can quickly reduce each the long empty lines to one empty line. Run:
You can quickly reduce the empty lines into one empty line with:
```
:g/^$/,/./-1j
@ -433,13 +433,12 @@ const three = 3;
console.log("three: ", three);
```
Let's break it down:
- `:g` is the global command
- `/^$/` is the pattern for an empty line. Recall that `^` means the beginning of the line and `$` the end of the line. `^$` matches an empty line (a line with zero characters long).
- `,/./-1` is the range for the `j` command. Since you don't pass a value for the starting range, it starts from the current line. You just learned earlier that `/./` is a pattern for a non-empty line. `,/./` is a range from the current line to the next non-empty line. The global command's range, `/^$/`, takes you to the first match on the line below `console.log("one: ", one);`. This is the current line. `/./` matches the first non-empty line, the line `const two = 2;`. Finally, `-1` offsets that by one line. The effective range for the first match is the empty line below the `console.log("one: ", one);` and the empty line above the `const two = 2;`.
- `j` is the join command `:j`. You can join all the lines given as its range. For example, `:1,5j` joins lines one through five.
Normally the global command accepts the following form: `:g/pattern/command`. However, you can also run the global command with the following form: `:g/pattern1/,/pattern2/command`. With this, Vim will apply the `command` within `pattern1` and `pattern2`.
Notice that you are passing a range (`,/./-1`) before the `j` command. Just because you are using a command-line command with the global command, does not mean you cannot give it a range. In this code, you are passing to the `j` command its own range to execute on. You can pass a range to any command while executing the global command.
With that in mind, let's break down the command `:g/^$/,/./-1j` according to `:g/pattern1/,/pattern2/command`:
- `/pattern1/` is `/^$/`. It represents an empty line (a line with zero character).
- `/pattern2/` is `/./` with `-1` line modifier. `/./` represents a non-empty line (a line with at least one character). The `-1` means the line above that.
- `command` is `j`, the join command (`:j`). In this context, this global command joins all the given lines.
By the way, if you want to reduce multiple empty lines into no lines, instead of using `,/./-1` as the range for `j` command, just use `,/./` as the range instead:
@ -447,7 +446,7 @@ By the way, if you want to reduce multiple empty lines into no lines, instead of
:g/^$/,/./j
```
Or simpler:
A simpler alternative:
```
:g/^$/-j

Loading…
Cancel
Save