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:
"\\(.*?\\)"
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).' '\1' '
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With