1

In emacs I'm using M-% to replace occurrences of b_abcd (of which there are only a few) with B_abcd (of which there are very many). Currently Emacs asks if it's OK to replace at each B_abcd, which takes a lot of time to wade through all B_abcds it finds.

How can I make Emacs ask only about replacing b_abcd occurrences?

2 Answers 2

1

Bind or set case-fold-search to nil.

C-h k M-% tells us:

M-% runs the command query-replace (found in global-map), which is an interactive compiled Lisp function in replace.el.

It is bound to M-%, <menu-bar> <edit> <replace> <query-replace>.

(query-replace FROM-STRING TO-STRING &optional DELIMITED START END BACKWARD REGION-NONCONTIGUOUS-P)

Probably introduced at or before Emacs version 1.3.

Replace some occurrences of FROM-STRING with TO-STRING.

As each match is found, the user must type a character saying what to do with it. For directions, type C-h at that time.

...

Matching is independent of case if both case-fold-search and search-upper-case are non-nil and FROM-STRING has no uppercase letters; if search-upper-case is nil, then whether matching ignores case depends on case-fold-search regardless of whether there are uppercase letters in FROM-STRING. Replacement transfers the case pattern of the old text to the new text, if both case-fold-search and case-replace are non-nil and FROM-STRING has no uppercase letters. (Transferring the case pattern means that if the old text matched is all caps, or capitalized, then its replacement is respectively upcased or capitalized.)

If your string to replace had uppercase letters and you wanted to always respect case then you would set case-fold-search to non-nil and search-upper-case to nil. In your case the string to replace has no uppercase letters, so it's sufficient to set case-fold-search to non-nil.

You can customize either or both of these variables (options), to make them have the values you want most often by default. That way, you will only need to set or bind them occasionally, for your less frequent uses. To customize them, use M-x customize-option.

1
  • Indeed. Thank you so much for your quick answer.
    – HarryH
    Oct 25, 2020 at 4:48
4

Use isearch (C-s), turn on case sensitivity (M-c) then launch replace (M-%) from there.

  • M-c runs the command isearch-toggle-case-fold
  • M-% runs the command isearch-query-replace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.