21

Suppose You have some text with words like these inside:

"foo" ... "bar" ... "file" ... "emacs"

and all you want to do is to replace " with ' '. I know it can be done only asking for the following replacement:

" -> ''

but suppose you would like to do it interactively and with regex, for more complicated case, how can it be done?

I explain myself better: I wish I could have a command, or a series of them, to work in this way:

  • Write a regex like "\\(.*?\\)" and see the results interactively (i.e. currently-visible matches are highlighted as you edit the regexp, and the highlighting is always updated as you make changes).
  • Once happy with the regexp, specify a replacement such as ' '\1' '
  • Interactively replace all matches
2
  • what do you mean "recursively"? Commented Jun 21, 2012 at 11:47
  • event_jr: I updated the question based on Luigi's answer, and rephrased that part in the process, as it did not appear to be relevant. Commented Jun 21, 2012 at 12:07

3 Answers 3

25

Finally I've found out!!!!! These are the steps to follow:

  • M-x isearch-forward-regexp

  • Insert your regex interactively. If you make a mistake you can use DEL or M-e and navigate through your regex to correct it.

  • Then after you've found what you want immediately press M-% and the replacement you like and start replacing.

So you can search and replace with regex in a total interactive and incremental fashion.

Sign up to request clarification or add additional context in comments.

3 Comments

Good find. On my installation isearch-forward-regexp is bound to C-M-s.
M-x isearch-describe-bindings (or C-s C-h C-h b) is well worth reading.
thank you for this. isearch-describe-bindings is also very helpful, though I wish I had some intuition about how I could have discovered that within Emacs. I continually find myself getting lost within Emacs's extensive documentation.
8

Does the command query-replace-regexp satisfy your requirements ? On most installations of Emacs it will be bound to C-M-%.

Since that doesn't satisfy OP's requirements, suggest looking also at re-builder. This function is not documented in the Emacs manual, or help system, it is just about documented in the Emacs Lisp manual.

3 Comments

query-replace-regexp it's not interactive, or better incremental. I'd like to have an incremental way to look at what it's matched
It didn't work so for me, it's neither interactive nor incremental. Do you have any idea why? I've downloaded emacs through apt-get. By the way I've posted the solution that works fine in my case.
(sorry, I'd just deleted my first comment while you were adding your reply). I hadn't understood that you wanted incremental match highlighting while you were building the regexp, which is why I was confused. I understand what you were after now.
8
  • Run M-x isearch-forward-regexp or C-M-s.

  • As you start typing a regex the first occurrence will be instantly highlighted. Attempting to move back through the regex using the cursor will cancel the search, so if you make a mistake you should use M-e to allow editing, make the change, then hit enter to continue the search.

  • When you're happy with the regex, press M-%, type a replacement string (or use the up key to access previously used replacement strings), then press RET to start interactive replacement.

  • For each occurrence, press y to replace once, n to skip, ! to replace all, RET to exit, or ? to see further options.

  • To repeat your last regexp search enter C-M-s then C-s.

Full details are in the manual page.

2 Comments

I really just wanted to add some detail to Luigi's answer, but since my edit was declined I'm posting it as a separate answer.
May the force be with you. With love,

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.