Vim, inverting words using text objects
Inverting two words followed by some characters
You have the following three words:
ARE HOW YOU
To invert the first two words you can use one of the following simple text ojbects solution:
Place the cursor inside the first word ARE and press the following key sequence:
dawwP
Or place the cursor inside the second word HOW and press the the following key sequence:
dawbP
Synthesis table:
The cursor is | You press |
---|---|
Inside first word | dawwP |
Inside second word | dawbP |
The result is the same.
The key sequence daw
, common to both solutions, is the text object.
you can read them as follows: Delete Around Word.
If you use the first solution you have to go to the next Word and Place the deleted word before it: wP
(note che upper P).
If you use the second solution you have to go to the Back word_ and Place_ the deleted word before it: bP
.
After both solutions you get the inversion of the first two words including the spaces:
HOW ARE YOU
Inverting two words NOT followed by some characters
You have, now, use another useful key command: the J
one.
ARE HOW
With the cursor on the second word you can use the following sequence:
diwOpJ
which means:
diw
: Delete Inner WordO
: begin e new line abOve the cursor and press Escp
: puts the text object in the upper (current) lineJ
: joins the upper line to the lower
After this solution you get the inversion of the two words with a space added:
HOW ARE
If you need to delete che remaining space after the second word you can press $x
in Normal Mode.
That’s all.
Thank You for your attention.