Emacs: Regular Expression Case Sensitivity
Case Sensitivity in Text Searching Commands
Case sensitivity for search is controlled by the variable case-fold-search
- case-fold-search
-
Variable. Configure case-sensitivity for commands that do text search, including commands that take a Emacs Regular Expression.
Value of t means smart-case. That is, if search term contain Capital letters, the search is case-sensitive, otherwise no. By default, the value is t.
Example:
dragon
matches {dragon, Dragon, DRAGON, draGON}Dragon
matches only Dragon.
If nil, search is case-sensitive.
- Alt + x
toggle-case-fold-search
-
toggles the variable case-fold-search
Tip: remember to toggle it back when you are done.
Case Sensitivity in Replacement, for Interactive Call
By default, the case of the replaced text is smartly dependent on the matched text.
For example, suppose your search string is here
, and your replacement string is dragon
.
Emacs will find any of {here
, Here
, HERE
}.
- when emacs found
here
, the replacement isdragon
- when emacs found
Here
, the replacement isDragon
- when emacs found
HERE
, the replacement isDRAGON
.
If you want the letter case of your replacement string be exactly as you have it, you need to set the variable case-replace to nil. You can do so by Alt + x set-variable
.
Change Case Sensitivity in Emacs Lisp Code
In elisp code, set case-fold-search like this:
(let ((case-fold-search nil)) (re-search-forward "ABC") ;; other code here )
For the replacement string,
the
replace-match
function has a option for it.
Emacs Regular Expression
- Regular Expression
- Regex Syntax
- About Quoting Regex
- Case Sensitivity
- How to Insert a Tab or Newline
- Wildcards vs Regex
- Emacs Regex vs Python, JavaScript, Java