When I do M-x query-replace
to replace all occurrences of v
by w
, then it also matches V
(uppercase V
). How to force emacs to only find lowercase v
?
-
"Case-insensitive" is the default behavior, where case doesn't matter in the search. If you want to only match one case, it's "case-sensitive" that you want. (I might miss something too, feel free to rollback the edit if needed)– T. VerronCommented May 29, 2015 at 5:04
-
@T.Verron I am convinced now, you are right.– NameCommented May 29, 2015 at 7:43
-
There's a clever way of doing this in this answer.– Hans LundmarkCommented Oct 14, 2022 at 8:41
1 Answer
(customize-set-variable case-fold-search nil)
Or bind that variable (option) in your own command that is otherwise just a wrapper around query-replace
. This has the advantage that it doesn't change the variable value for general use, outside of query-replacing.
See also variable case-replace
, which controls case for the replacement text.
See also C-h f query-replace
, where it says, for example:
Matching is independent of case if
case-fold-search
is non-nil and FROM-STRING has no uppercase letters. Replacement transfers the case pattern of the old text to the new text, ifcase-replace
andcase-fold-search
are non-nil and FROM-STRING has no uppercase
See also the comments below, and Emacs bug #20687, which show how you can patch perform-replace
to let you toggle case folding during query-replace
.
Note too that the existing code for perform-replace
binds case-fold-search
in this way (note the variables on which it depends):
(case-fold-search (if (and case-fold-search search-upper-case)
(isearch-no-upper-case-p from-string regexp-flag)
case-fold-search))
-
1It would have been nice to change
case-fold-search
on the fly as we can do duringisearch
using theM-c
binding. But unfortunatelyquery-replace
does have have its mode map. Commented May 28, 2015 at 20:38 -
3@kaushalmodi: Normally, it would be as simple as adding a key binding for that to
query-replace-map
:(defun toggle-case () (interactive) (setq case-fold-search (not case-fold-search)))
and(define-key query-replace-map "C" 'toggle-case)
. But the handling of keys in that map is hard-coded inperform-replace
. You might want toM-x report-emacs-bug
, to get theperform-replace
code to be more open-ended, so keys can be added to the map. It should at least have a fallback clause that just invokes the cmd bound to the key.– DrewCommented May 28, 2015 at 21:03 -
1Thanks! Not sure why I didn't find
query-replace-map
before. Commented May 28, 2015 at 21:13 -
1I've filed that bug report now (#20687). The solution is in that bug report: Just modify
perform-replace
to add an additionalcond
clause that invokes the key you've defined inquery-replace-map
. Put the new clause just before the catch-allt
clause. This is the new clause:(def (call-interactively def))
. Couldn't be simpler.– DrewCommented May 28, 2015 at 21:14 -
(customize-set-variable case-fold-search nil)
gives the errorcustomize-set-variable: Attempt to set a constant symbol: nil
. Also I tried both(setq case-fold-search nil)
and(setq case-replace nil)
but the query still match the uppercase V.– NameCommented May 28, 2015 at 21:45