Something like this should work. We'll use capture groups (\(\)
) and back-references (\1
,\2
) to do the swap, and the right pattern to find all the right places.
This one I wrote from scratch, but if I wasn't sure to get it right I would first search interactively (or using :global
) until I got the right pattern, then use :%substitute//...
to re-use the pattern.
%substitute/\\operation{\(e_[^}]*\)}{\([^}]*\)}/\\operation{\2}{\1}
Add /g
if you have more than one on a line.
We use [^}]*
to simulate a non-greedy .*
(vim can do this with .\{-}
), but in this case we really only want until the next }
. If A
and B
can contain nested {}
, which is generally true in LaTeX, you might have better luck with .*
or .\{-}
, or you might need something far more sophisticated. An example:
global/\\operation{e_/normal! 0f{di{;vi{p%,p
Breakdown:
:global/\\operation{e_/
: for every line matching the pattern
:normal!
: do these normal mode key-strokes without mappings
0f{di{
: delete the first argument
;vi{p
: paste it over the second
%,p
: paste the second argument back in the first spot
This will handle {}
oddities more readily, since we let vim do the complex nested-matching for us (regular expressions aren't powerful enough).
Realistically, I would record a macro (probably with trial and error):
qq0f{di{;vi{p%,pq
and then do
global/\\operation{e_/normal! @q