I am using combination of following solutions in order to find-and-replace matched text in all Python files:

I observe that when I want to replace capital words like "HELLO_WORLD" , it also replaces hello_world string as well.

(setq case-fold-search nil)

(defun find-and-replace ()
  (message "press t to toggle mark for all files found && Press Q for Query-Replace in Files...")
  (find-dired (vc-git-root (buffer-file-name)) "-name \\*.py -o ! -name flycheck_\\*.py ! -name __init__.py ! -name \".*\" ! -name build ! -path \\*/.eggs/\\*"))

M-x find-and-replace ; then t to toggle marked/unmarked files (thus marking them all, since none were marked). Then I use Q to use query-replace on the marked files. And enter HELLO_WORLD to replace with WORLD_HELLO.

How can I fix find-and-replace to work in case-sensetive?

  • Did you try setting (or binding) case-fold-search to nil?
    – Drew
    Commented May 28, 2022 at 19:27
  • Sorry to add it into my question, I had already have (setq case-fold-search nil) in my init file
    – alper
    Commented May 28, 2022 at 21:41
  • Don't be sorry. Thanks for specifying that in the question.
    – Drew
    Commented May 28, 2022 at 23:00
  • 1
    (setq case-fold-search nil) is wrong in the init file. You must use (setq-default case-fold-search nil) since case-fold-search is a buffer-local variable. The setting (setq case-fold-search nil) just affects the single buffer where the init file is evaluated.
    – Tobias
    Commented May 31, 2022 at 16:33
  • @Tobias (setq-default case-fold-search nil) solved my probles, please feel free to make it and answer. In generat I always use setq instad of setq-default like (setq message-log-max t) in my init file. Should I use setq-defaultinstead ?
    – alper
    Commented Jun 1, 2022 at 9:20

1 Answer 1


As already mentioned in the comments the variable case-fold-search is buffer-local.

The following line in your init file sets this variable only for the buffer to nil where the init file is evaluated:

(setq case-fold-search nil)

To change the default value that is valid in all buffers where case-fold-search is not explicitly set you need to set the default value of case-fold-search:

(setq-default case-fold-search nil)
  • Sorry for the late command, can this only applied for the find-and-replace but during I-search ignore case sensetive search?
    – alper
    Commented Jun 5, 2022 at 14:00

Your Answer

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

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