I am using following answer to search and replace a word in the entire buffer:

(defun query-replace-region-or-from-top ()   
    (let ((orig-point (point)))
      (if (use-region-p)
          (call-interactively 'query-replace)
          (goto-char (point-min))
          (call-interactively 'query-replace)))
      (message "Back to old point.")
      (goto-char orig-point)))) 

(global-set-key "\C-x\C-r"  'query-replace-region-or-from-top)```

When I apply query-replace-region-or-from-top, for example emacs into emacs_world it also changes EMACS into EMACS_WORLD.

How can I make query-replace-region-or-from-top case-sensitive?

  • Does this answer your question? How do I search/replace with case sensitive search?
    – Drew
    Commented Oct 4, 2021 at 15:29
  • @Drew yes sir , NickD's answer also helps how can I use it
    – alper
    Commented Oct 4, 2021 at 15:49
  • The question seems to be a duplicate of that question, in which case it should be deleted.
    – Drew
    Commented Oct 4, 2021 at 17:04
  • Another answer: stackoverflow.com/questions/5346107/… // small example is helpful to understand how can I use it under the defun function like using (let ((case-fold-search nil))
    – alper
    Commented Oct 4, 2021 at 20:44

1 Answer 1


You need to let-bind case-fold-search to nil:

(defun query-replace-region-or-from-top ()   
  (let ((case-fold-search nil))  
      (goto-char orig-point)))))

See the doc string for case-fold-search with C-h v case-fold-search:

case-fold-search is a variable defined in ‘src/buffer.c’.

Its value is t

Automatically becomes buffer-local when set. You can customize this variable. Probably introduced at or before Emacs version 18.

Non-nil if searches and matches should ignore case.

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.