Tuesday, December 1, 2009

Re: How to switch two strings?

> I frequently need to switch two strings. For example, if I want to
> switch 'a' and 'b', I shall use the following three commands. I'm
> wondering if there is a more convenient way to do so.
>
> :%s/b/c/g
> :%s/a/b/g
> :%s/c/a/g

I'm not sure how much more/less convenient it is, but you can do
things like

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

to do swaps like that...it works for any fixed string:

:%s/foo\|bar/\=submatch(0)=='foo'?'bar':'foo'/g


-tim


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment