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. Verron– T. Verron2015-05-29 05:04:45 +00:00Commented May 29, 2015 at 5:04
-
@T.Verron I am convinced now, you are right.Name– Name2015-05-29 07:43:13 +00:00Commented May 29, 2015 at 7:43
-
There's a clever way of doing this in this answer.Hans Lundmark– Hans Lundmark2022-10-14 08:41:05 +00:00Commented 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-searchis non-nil and FROM-STRING has no uppercase letters. Replacement transfers the case pattern of the old text to the new text, ifcase-replaceandcase-fold-searchare 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-searchon the fly as we can do duringisearchusing theM-cbinding. But unfortunatelyquery-replacedoes have have its mode map.Kaushal Modi– Kaushal Modi2015-05-28 20:38:56 +00:00Commented 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-replacecode 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.Drew– Drew2015-05-28 21:03:07 +00:00Commented May 28, 2015 at 21:03 -
1Thanks! Not sure why I didn't find
query-replace-mapbefore.Kaushal Modi– Kaushal Modi2015-05-28 21:13:31 +00:00Commented May 28, 2015 at 21:13 -
1I've filed that bug report now (#20687). The solution is in that bug report: Just modify
perform-replaceto add an additionalcondclause that invokes the key you've defined inquery-replace-map. Put the new clause just before the catch-alltclause. This is the new clause:(def (call-interactively def)). Couldn't be simpler.Drew– Drew2015-05-28 21:14:13 +00:00Commented 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.Name– Name2015-05-28 21:45:48 +00:00Commented May 28, 2015 at 21:45