October 2012
346 pages
7h 40m
English
| Tip 95 | Swap Two or More Words |
We can devise a substitute command that swaps all occurrences of one word with another and vice versa by using the expression register and a Vim script dictionary.
Take this excerpt:
| substitution/who-bites.txt | |
| | The dog bit the man. |
Suppose that we want to swap the order of the words “dog” and “man.” We could, of course, use a succession of yank and put operations, as demonstrated in Swap Two Words. But let’s consider how we would go about doing this with the substitute command.
Here’s a naïve attempt at a solution:
| => | :%s/dog/man/g |
| => | :%s/man/dog/g |
The first command replaces the word “dog” with “man,” leaving us with the phrase “the man bit the man.” Then the second command replaces both occurrences ...
Read now
Unlock full access