Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write only one pattern to exchange two strings in two-ways in vim

Tags:

vim

xxx <=> zzz


Original text:

xxxyyyzzz

Run command:

:s/???/???/

It becomes:

zzzyyyxxx

Run the same command above:

:s/???/???/

Back to the original text:

xxxyyyzzz


just like toggle case(~)

aBCdeFg <=> AbcDEfG

like image 352
kev Avatar asked Jun 04 '11 16:06

kev


2 Answers

Take a look at this: Can I substitute multiple items in a single regular expression in VIM or Perl?

like image 152
Chris J Avatar answered Nov 15 '22 08:11

Chris J


Try this:

:s/(...)(yyy)(...)/\3\2\1/
like image 20
MRAB Avatar answered Nov 15 '22 09:11

MRAB