Suppose I have a buffer containing
a
b
c
How can I use a single search and replace command to obtain the following (cyclic) permutation?
b
c
a
In this question, I found one can use the "bar" operator |
to execute multiple commands at once. However, it seems these commands are applied consecutively, as the command
:%s/a/b/ | %s/b/c/ | %s/c/a/
gives me
a % a -> b -> c -> a
a % b -> c -> a
a % c -> a
which is not what I want. I looked at this guide and found it's possible to match from several options using the syntax \(a\|b\|c\)
and subsequently refer back to the matched objects using \1
, \2
and \3
.
I feel this is getting close, but something like
:%s:\(a\|b\|c\):\3\2\1:
does not cut it -- it leaves my original buffer invariant, probably since there's only one match on each line, and that's the only one of the three options that's substituted back. Ideally, I guess I would like to specify a substitution for each of the backreferences, so something like "\1
goes to b
" etcetera. Is that possible?