Question

So this problem must be as old as programming itself. If I have this block of code :

Arequest : A.state == IDLE & B.state == OUT     -> A.state == RUNNING
           A.state == IDLE & B.state == IN      -> A.state == POLLING
           A.state == IDLE & B.state == RUNNING -> A.state == WAITING

And the next block of code I want to type is this (A is swapped with B)

Brequest : B.state == IDLE & A.state == OUT     -> B.state == RUNNING
           B.state == IDLE & A.state == IN      -> B.state == POLLING
           B.state == IDLE & A.state == RUNNING -> B.state == WAITING

So basically any foo(A,B) into foo(B,A). How do I do that with VIM ? Been searching around a lot already, to no avail. Dr Chip's Swapstrings will only swap occurrences of A and B that are between separators (dots, parentheses, spaces, ...) but Arequest (where the string to swap is part of a bigger string) won't be swapped into Brequest. Tim Pope's Abolish won't do either (same reason).

Was it helpful?

Solution 2

Since you mention Abolish I figured I would show you how that is done:

%S/{A,B}/{B,A}/wg

The secret is to use the w flag which means it only changes on words. The only missing part is to change Arequest to Brequest which can be done via :Subvert as well or by hand which ever is easier.

%S/{A,B,Arequest}/{B,A,Brequest}/wg

OTHER TIPS

Create a macro as you execute the changes once and move to next line and restart. Something like this:

qq/A<CR>cwB<ESC>/B<CR>cwA<ESC>/A<CR>cwB<ESC>^jq

run your macro with @q first time then repeat @@ until done

If your system has tr command, use that.

:%!tr AB BA

BTW, A in WAITING will be replaced.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top