Emacs: Find Replace, by Regex
Interactive Find Replace, by Regex Pattern
Alt+x query-replace-regexp
- Find replace by Emacs: Regular Expression
- Works on text selection, if none, start from cursor position to end of buffer.
Replace String Special Characters
There are special syntax in the replacement string prompt.
\&-
whole matched text.
\n-
n is a digit. Means the nth capture in regex.
\?-
prompt user to type text for each replacement.
\,lispExpr-
execute the lispExpr and use its value as replacement string.
Inside the lispExpr,
\&→ is the whole match (string type).\n→ is the nth capture (string type).\#&→ whole match, converted to number. (assume the match is a number)\#n→ nth capture converted to a number. (assume the match is a number)\#→ the number of replacements done so far (starting with zero).
If lispExpr is a Elisp: Symbol , write a space after it.
Example
Find Replace by Regex, with Word Boundary
Ctrl+u Alt+x query-replace-regexp
this saves you time of typing the regex word boundary chars.
e.g.
\bsomething\b
Find Replace All, by Regex, in One Shot
Alt+x replace-regexp
- Find replace by Emacs: Regular Expression
- Works on text selection, if none, start from cursor position to end of buffer.