Elisp: Find Replace Text in Buffer
Functions for Find Replace in a Region
replace-string-in-region
-
(replace-string-in-region STRING REPLACEMENT &optional START END)
Replace string in a region. No regex. START default to current cursor position. END default to end of buffer.
return the count of placement, or nil if none.
Comparisons and replacements are done with fixed case.
new in Emacs 28 (Released 2022-04)
🛑 WARNING: if you need to do this again, remember that the END position may have changed.
(replace-string-in-region "some" "thing" ) ;; some thing in water
replace-regexp-in-region
-
(replace-regexp-in-region REGEX REPLACEMENT &optional START END)
Replace Regular Expression in a region. START default to current cursor position. END default to end of buffer.
return the count of placement, or nil if none.
Comparisons and replacements are done with fixed case.
in REPLACEMENT, the following has special meaning:
\&
→ the original matched text.\N
nth capture. it is empty string if capture didn't match.\\
means one backslash.
new in Emacs 28 (Released 2022-04)
🛑 WARNING: if you need to do this again, remember that the END position may have changed.
(replace-regexp-in-region "[0-9]+ " "" ) ;; i have 5 cats ;; become ;; i have cats
General Method to Do Find Replace Text in Buffer
The general method to do text replacement in current buffer, is via
search-forward
,
re-search-forward
etc
〔see Elisp: Search Text Functions〕
, then call
replace-match
or other function of
Match Data.
And if you need to restrict the find replace in a region, wrap it with
narrow-to-region
.
〔see Elisp: Save narrow-to-region〕
The emacs 28 new functions
replace-string-in-region
,
replace-regexp-in-region
are convenient wrapper of this method.
(let ((case-fold-search nil)) ;; set case-fold-search to t, if you want to ignore case (goto-char (point-min)) (while (search-forward "myStr" nil t) (replace-match "myReplaceStr")) ;; repeat for other string pairs ;; use re-search-forward if you want regex )
Find Replace in a Region
;; idiom for multiple string replacement within a region (save-restriction (narrow-to-region pos1 pos2) ;; plain text find replace (goto-char (point-min)) (while (search-forward "myStr1" nil t) (replace-match "myReplaceStr1")) ;; regex find replace (goto-char (point-min)) (while (re-search-forward "someregex" nil t) (replace-match "myReplaceStr1")) ;; more find replace here )
Replace Match
replace-match
-
(replace-match NEWTEXT &optional FIXEDCASE LITERAL STRING SUBEXP)
Replace the matched pattern of previous text search function call. ( •
search-forward
•search-backward
•re-search-forward
•re-search-backward
)- NEWTEXT is replacement string.
- If FIXEDCASE is true, no case conversion in replacement.
- If LITERAL is true, no backslash interpretation.
in replace string NEWTEXT, backslash has the following special meaning, unless LITERAL is true:
\&
→ whole matched text.\N
→ nth captured group.\\
→ literal backslash.
🛑 WARNING: each backslash should be double in string.
(let ((case-fold-search nil)) (re-search-forward "x\\([0-9]+\\)" nil t) (replace-match "ID \\1")) ;; x803 ;; becomes ;; ID 803
Case Sensitivity in Replacement
To control letter case of the replacement, use the optional arguments in replace-match
function.
(replace-match NEWTEXT &optional FIXEDCASE LITERAL STRING SUBEXP)
Use t for FIXEDCASE.
By default, the letter case used in replacement is smart. If the found text is First-cap or ALLCAPS, the replacement is accordingly.
Match Data
the begin/end positions of match, or capture, are stored in match-data
.
Elisp, Regex in Lisp Code
- Elisp: Regular Expression
- Elisp: Regex Functions
- Emacs: Regular Expression Syntax
- Elisp: Regex Backslash in Lisp Code
- Elisp: Case Sensitivity (case-fold-search)
- Elisp: Find Replace Text in Buffer
- Elisp: Match Data (Regex Result)
- Elisp: Unicode Escape Sequence
- Elisp: Convert Regex to Lisp Regex String
- Elisp: How to Test Regex
- Elisp: Regex in Readable Syntax, Package Rx
- Elisp: Regex Named Character Class and Syntax Table
- Emacs Regex vs Regex in Python, JavaScript, Java
Elisp, text processing functions
- Elisp: Cursor Position Functions
- Elisp: Move Cursor
- Elisp: Text Editing Functions
- Elisp: Search Text
- Elisp: Find Replace Text in Buffer
- Elisp: Mark, Region, Active Region
- Elisp: Cut Copy Paste, kill-ring
- Elisp: Get Buffer String
- Elisp: Functions on Line
- Elisp: thing-at-point
- Elisp: Get Text Block 🚀
- Elisp: Save narrow-to-region