Swap or more Words
The following file
Suppose that we want to swaps the order of the words "dog" and "man."
Suppose we are going to swap the two words of dog and man in the text, what should we do? We're trying to use the following command
:%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 of ' man ' with ' dog, ' giving us ' thedog bit the dog. Clearly, we have the to try harder.
After executing the first command, we get a
"The man bit, the man. "
After executing the second command, we get a
"Thedog bit the dog."
A two-pass solution is no good, so we need a substitute command that works in a single pass. The easy part was writing a pattern that matches both ' dog ' and ' man ' (think about it). The tricky part was writing an expression, that accepts either of these words and returns, the other one. Let's solve this part of the puzzle first.
Dual-threaded commands No, we need a replacement command to execute only once. The simplest part is to write a pattern that matches the dog or man. The hard part is how to write an expression that accepts one of the two words and then returns another word. We'll take care of the hardest part first.
Return the other Word
We don ' t even has to create a function to get the job done. We can do it with a simple dictionary data structure by creating-key-value pairs. In Vim, try typing the following:
Here we do not need to use the function to implement, we can simply through the dictionary data results to complete, by making two key_value pairs. In Vim, we can do this:
:let swapper={"dog":"man","man":"dog"} :echo swapper["dog"] man :echo swapper["man"] dog
Let's create a dictionary swapper, enter man for Swapper, return dog, enter dog, return man
Match Both Words
Did you figure out the pattern? Here it is:
The pattern of matching man or dog at the same time is:
/\v(<man>|<dog>)
This pattern simply matches the whole word "man" or the whole word "dog." The parentheses serve to capture the matched text so, we can reference it in the replacement field.
This pattern matches the whole word man or dog. The outer brackets are used to get the matching text so that they are referenced in the replacement field.
All Together Now
For the replacement, we'll have evaluate a little bit of Vim script. That means using the \=
item in the replacement field. This time, we won ' t bother assigning the dictionary to a variable, we'll just create it inline for a single use.
in order to replace it, we will use the Vim script. This means that we will use \=
in the replacement domain. Here, we don't have to assign a dictionary to a variable, we just create one online.
Normally we could refer to captured the text using Vim ' s \1, \2
(and so on) notation. But in Vim script, we had to fetch the captured text by calling the Submatch () function (See:h submatch ()).
In general mode we refer to the matched text through \1, \2
, but in a vim script we can only get the matching text by calling the function Submatch ().
When we put the everything together, this is the what we get:
We can get the first two commands together:
/\v(<man>|<dog>):%s//\={"dog":"man","man":"dog"}[submatch(1)]/g
[Practical.vim (2012.9)]. Drew.Neil.Tip95 Study Summary