11

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?

12
  • There may be a single-step way, but your first way is not bad, IMO. And if you choose the replacement string right then you can just do no-query replacement for the second pass.
    – Drew
    Sep 17, 2016 at 13:48
  • @Drew For regular query-replace, I know that it is usually faster to M-% 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 of str1. Ideally, I'd like text inversion to be just as fast, so that I can use it without thinking about it as well.
    – T. Verron
    Sep 17, 2016 at 14:00
  • Someone will provide a simple answer. You can no doubt do what you ask, e.g., by using a Lisp expression for the replacement, referencing the text to be replaced. E.g., use something similar to the swap idiom (setq a (prog1 b (setq b a))). And there may even be a simpler way.
    – Drew
    Sep 17, 2016 at 15:05
  • 2
    I don't find \,(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.
    – politza
    Sep 19, 2016 at 17:01
  • 2
    Why not \(aaa\)\|bbb ?
    – politza
    Sep 21, 2016 at 5:05

2 Answers 2

8

Here is a small command that will do this:

(defun query-swap-strings (from-string to-string &optional delimited start end)
  "Swap occurrences of FROM-STRING and TO-STRING."
  (interactive
   (let ((common
          (query-replace-read-args
           (concat "Query swap"
                   (if current-prefix-arg
                       (if (eq current-prefix-arg '-) " backward" " word")
                     "")
                   (if (use-region-p) " in region" ""))
           nil)))
     (list (nth 0 common) (nth 1 common) (nth 2 common)
           (if (use-region-p) (region-beginning))
           (if (use-region-p) (region-end)))))
  (perform-replace
   (concat "\\(" (regexp-quote from-string) "\\)\\|" (regexp-quote to-string))
   `(replace-eval-replacement replace-quote (if (match-string 1) ,to-string ,from-string))
   t t delimited nil nil start end))
7
  • Thank you, that's way more elaborate than something I would have written myself! Maybe you could elaborate on the implemented features? I see that with prefix argument you go backwards, that it performs the replace only in region if needed... Or is it just a rewriting of query-replace for the inversion, so that it retains all its features? It doesn't match 100% the definition in my emacs, but I'm still with 24.5...
    – T. Verron
    Sep 20, 2016 at 7:58
  • @T.Verron This is just a standard body from query-replace family of functions from the latest version, so it retains all features. To the end of the function I added my recommended way of swapping strings mentioned in Emacs manual.
    – link0ff
    Sep 20, 2016 at 8:26
  • I probably accepted too fast. The function only works here (emacs 24.3) if I comment out backward and region-noncontiguous-p from the call to perform-replace. Is it a general problem, or does it mean that the interface to perform-replace has changed between versions?
    – T. Verron
    Oct 14, 2016 at 7:30
  • Yes, the interface has changed with these 2 arguments added in the latest version, so they don't exist in earlier ones,
    – link0ff
    Oct 14, 2016 at 7:35
  • 2
    This solution also seems to be fine now.
    – Name
    Oct 14, 2016 at 8:10
8

Install plur

and run the command plur-query-replace

and input {foo,bar} and its replacement {bar,foo}

Hit y to replace the occurrences as desired.

There are also non-interactive, and isearch-like, variants of this command.

2
  • Thanks! plur's purpose is also something I have sometimes been looking for, I will definitely have a look at it. My only hesitation stems from the comment of the author of the package above... ;)
    – T. Verron
    Sep 20, 2016 at 7:55
  • 1
    plur requires emacs 24.4.
    – T. Verron
    Oct 14, 2016 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.