0

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 ()
  (interactive)
  (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?

alper
  • 1,238
  • 11
  • 30
  • Did you try setting (or binding) `case-fold-search` to `nil`? – Drew May 28 '22 at 19:27
  • Sorry to add it into my question, I had already have `(setq case-fold-search nil)` in my init file – alper May 28 '22 at 21:41
  • Don't be sorry. Thanks for specifying that in the question. – Drew May 28 '22 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 May 31 '22 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-default`instead ? – alper Jun 01 '22 at 09:20

1 Answers1

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)
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • Sorry for the late command, can this only applied for the `find-and-replace` but during `I-search` ignore case sensetive search? – alper Jun 05 '22 at 14:00