Elisp: Case Sensitivity (case-fold-search)

By Xah Lee. Date: .


Case sensitivity for search is controlled by the variable case-fold-search


Variable. Control case-sensitivity for functions that do text search, including commands that take a Emacs Regular Expression.

  • Value of t means smart. That is, if search term contain Capital letters, the search is case-sensitive, otherwise no.
  • nil means search is case-sensitive.
  • By default, the value is t.

Example of case-fold-search true:

  • dragon matches {dragon, Dragon, DRAGON, draGON}
  • Dragon matches only Dragon.

the following functions are effected:

  • search-forward
  • re-search-forward
  • string-match
  • replace-regexp-in-string
  • not skip-chars-forward

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

Case Sensitivity in Replacement, Text Replace Functions

replace-match function has a option for it.

Emacs: Case Sensitivity in Text Search

Emacs Lisp, Regex in Lisp Code