# Calling all emacs experts

**URL:** <https://boards.straightdope.com/t/calling-all-emacs-experts/267310>\
**Category:** Factual Questions\
**Created:** [October 3, 2004, 11:31am UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310 "2004-10-03T11:31:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scarf-Ace](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Scarf-Ace](https://boards.straightdope.com/u/Scarf-Ace)\
**Post date:** [October 3, 2004, 11:31am UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/1 "2004-10-03T11:31:30Z")

</div>

Anyone know how I can use regular expressions in emacs to change all a’s to b’s and all b’s to a’s in one expression?

---

<div class="post-metadata">

**Author:** ![Tom\_P](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Tom\_P](https://boards.straightdope.com/u/Tom_P)\
**Post date:** [October 3, 2004, 12:27pm UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/2 "2004-10-03T12:27:32Z")

</div>

> [@Scarf-Ace](#):
>
> Anyone know how I can use regular expressions in emacs to change all a’s to b’s and all b’s to a’s in one expression?

Unless there is a specific pattern to the a’s and the b’s, I do not think so. The simplest would probably be to change all the a’s to something that doesn’t exist in the document, then change all the b’s to a’s, and finally change the “somethings” to a’s.

Waiting for someone smarter to tell me I’m wrong. I’ll never learn all those commands

---

<div class="post-metadata">

**Author:** ![praxim](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@praxim](https://boards.straightdope.com/u/praxim)\
**Post date:** [October 3, 2004, 2:01pm UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/3 "2004-10-03T14:01:19Z")

</div>

If you’re only looking to change a single letter, then you can select the entire buffer and replace its contents using “tr ab ba” (C-x h C-u M-| tr ab ba RET). (Assuming, of course, you’re running on an appropriately UNIX-y machine; if you’re not, you should be flogged. ;))

---

<div class="post-metadata">

**Author:** ![Scarf-Ace](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Scarf-Ace](https://boards.straightdope.com/u/Scarf-Ace)\
**Post date:** [October 3, 2004, 2:20pm UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/4 "2004-10-03T14:20:16Z")

</div>

Thanks I did it the way you suggested Tom, but i’m surprised there isn’t a command to do it normally.

As for:

> [@praxim](#):
>
> tr ab ba" (C-x h C-u M-| tr ab ba RET)

I’m not even going to type that - it looks like a formula to summon some hideous beast 🙂

---

<div class="post-metadata">

**Author:** ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)\
**Post date:** [October 4, 2004, 5:57pm UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/5 "2004-10-04T17:57:14Z")

</div>

I don’t think you can do it with **query-replace-regexp** , but Emacs has an extension **query-replace-regexp-eval** which allows an arbitrary Lisp expression to be evaluated to find the replacement text. You could, for example, type  
**M-x query-replace-regexp-eval  
[ab]  
(if (string= \0 “a”) “b” “a”)**  
to swap a and b. (This is a query function; just type “!” to immediately make all swaps.) Most of the time the swap-into-an-unused-string version is probably quicker though.

---

<div class="post-metadata">

**Author:** ![Scarf-Ace](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Scarf-Ace](https://boards.straightdope.com/u/Scarf-Ace)\
**Post date:** [October 4, 2004, 8:01pm UTC](https://boards.straightdope.com/t/calling-all-emacs-experts/267310/6 "2004-10-04T20:01:59Z")

</div>

Ah thanks a lot, that will save me a lot of time. 🙂

Its about time I started learning Lisp anyway.
