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
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. ;))
Thanks I did it the way you suggested Tom, but i’m surprised there isn’t a command to do it normally.
As for:
I’m not even going to type that - it looks like a formula to summon some hideous beast
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.
Ah thanks a lot, that will save me a lot of time.
Its about time I started learning Lisp anyway.