> If I want to replace a to b, replace b to c, is it possible to do this 
> two things in one line?


Well, you can use the pipe to concat commands, so you can do:

        :%s/b/c/g|%s/a/b/g

However, if they overlap, it gets trickier.  In that case (such
as swapping "a" and "b"), you can use something like

        :%s/a\|b/\={'a':'b', 'b':'a'}[submatch(0)]/g

in Vim7.

To use that trick, both "a" and "b" should be "actual" values,
rather than regexps unless you can enumerate all values that the
regexp will match (because you need to look up the result in the
dictionary).  This works for arbitrary remapping of however many
pieces you want.  If you only want two pieces and are using
pre-vim7 where dictionaries were introduced, you can try
something like

        :%s/a[xyz]\|b/\=submatch(0)=~'a[xyz]' ? 'b' : 'a'/g

This allows you to have at least one regexp for matching purposes
(that you repeat in the conditional for the trinary operator).
The above example looks for any regexp matching "ax, ay, or az"
or "b" and then replaces any of the "a[xyz]" matches with "b" and
replaces any "b" matches with "a".

All sorts of odd things can be done here.  However, it does
demonstrate some of the power of \=

        :help sub-replace-special

Hope this gives you something to work with and allows you to do
crazy-cool editing stuff in the future.

-tim





Reply via email to