I just asked a related question (setq question) but it’s distinctly different, so I decided to branch off with this question.
In my .emacs file, I define a key binding to the replace-string command:
(define-key global-map "\C-r" 'replace-string)
replace-string does basic search and replace. Assuming the first letter of the search string is lowercase, if the case-fold-search is nil then replace-string does case-sensitive search, otherwise it does case-insensitive search.
The problem is that case-fold-search controls the “case-sensitiveness” of both “search” (like the search-forward command) and “search and replace” (like the replace-string command).
The question is how do I make JUST the replace-string command (or anything C-r is bound to) case-sensitive, leaving the search-forward case-insensitive as it is by default.
Perhaps I would need to set case-fold-search to nil just for the replace-string command, but I’m not sure how to do that.
Put this in your .emacs:
This does exactly what you said, set
case-fold-searchtoniljust forreplace-string.In fact this is almost exactly the example in the Emacs Lisp reference manual.
Edit on 2021-11-02: as the link above indicates,
defadviceis no longer the recommended way to implement this. The new recommended implementation would be