I often find myself doing
:s/foo/bar/g
*move to different line*
:s/bar/foo/g
on different lines. Is there an easy way to swap them around so that I can execute the second version quickly?
I often find myself doing
:s/foo/bar/g
*move to different line*
:s/bar/foo/g
on different lines. Is there an easy way to swap them around so that I can execute the second version quickly?
You could try the Abolish plugin (git homepage):
:Subvert/{foo,bar}/{bar,foo}/g
Without plugin:
:%s/foo\|bar/\=submatch(0) ==# 'foo' ? 'bar' : 'foo'/g
The quick use once only option is to do the following
:s/~/<c-r>//g<cr>
~ matches the last substitution and <c-r>/ will insert the current search string from the "/ register. Therefore flipping the substitution. A word of warning is that ~ can only be used once because after the substitution it will be changed. Also doing a search between substitutions will result in the "/ register changing.
As an alternative you could try to use the command-line window to edit the command like text in any other window.
Use q: to open the command-line window from normal mode or press ctrl-f from the command line (assuming the default setting for 'cedit').
Drew Neil has a vimcasts episode that deals with refining search patterns via the command-window which is similar.
:h /~
:h c_CTRL-R
:h quote/
:h cmdwin
:h q:
:h 'cedit'