I frequently find myself trying to replace e.g. foo
with bar
and bar
with foo
in a buffer.
The way I usually do it is either:
- 3 query-replaces:
aaa -> @@@
,bbb -> aaa
,@@@ -> bbb
- give up on automation and just do the replacement by hand
I suppose that it is possible to get something working using the ,
syntax in query-replace-regexp
to test if the matched string is aaa
or bbb
. But the resulting syntax would be too cumbersome, so I never really tried it.
The thing is, given how common a task it is, I think there must be a better, single-step, way of doing it, either built-in or in an existing package. Is there?
query-replace
, I know that it is usually faster toM-% str1 str2
than to move the point and do the change by hand. It's nice, because I don't have to waste time thinking about what will be fastest, even if there is only one occurrence ofstr1
. Ideally, I'd like text inversion to be just as fast, so that I can use it without thinking about it as well.(setq a (prog1 b (setq b a)))
. And there may even be a simpler way.\,(if \1 "b" "a")
especially ugly (against a regexp of\(a\)\|b
), but still the words have to be typed twice, if that bothers you.\(aaa\)\|bbb
?