Emacs makes assumptions about what constitutes an adequate match for a search. In particular, AFAICT, it disregards whitespace differences, as well as differences in case.

During an incremental search, it is possible to toggle these settings. For example, M-s SPC toggles whitespace matching.

But is there something I can put in my .emacs file such that the default initial settings are the strict ones? (I.e. search for the exact string I have typed in the minibuffer.)

Just to be clear, I still want to be able to toggle these settings. (In fact, my preference would be to selectively adjust these settings for the duration of a search, rather than turning them on or off for the remainder of the session.)

  • 1
    C-h k C-s says this: In incremental searches, a space or spaces normally matches any whitespace defined by the variable search-whitespace-regexp; see also the variables isearch-lax-whitespace and isearch-regexp-lax-whitespace. Setting these variables in your .emacs should change defaults. (Whenever I want to customize an Emacs feature, I use C-h k, C-h f, and C-h v to learn more about it. Most of the time it works very well.) – Constantine Feb 6 '16 at 18:51
  • @Constantine: Actually, this suggestion works for whitespace, but not for case-fold, and not just because the documentation for C-s says nothing about it. I find that (1) whereas M-s SPC has a persistent effect on how whitespace is handled in searches, the effect of M-s c lasts only for the duration of the search; and (2) the search is initially case insensitive irrespecitve of the value I assign to the variable isearch-case-fold-search. I've tested this in an Emacs session that I started with emacs -Q -l /dev/null, so I don't think it's due to some weird config. (v24.4.1, Debian) – kjo Feb 6 '16 at 19:25
  • @Constantine: OK, I get it. Even though the variable isearch-case-fold-search does exist, despite its name, it is not really the counterpart of isearch-lax-whitespace with respect to case-fold. That variable is case-fold-search. – kjo Feb 6 '16 at 19:31
  • Do you mind summarizing what worked for you in an answer? (Assuming that you have a solution.) Thanks! – Constantine Feb 6 '16 at 21:08
  • @Constantine: done – kjo Feb 6 '16 at 21:17

This is what I finally put in my .emacs

(setq isearch-lax-whitespace nil)
(setq isearch-regexp-lax-whitespace nil)

;; the next setting affects both isearch-* and isearch-*-regexp; also, according
;; to its documentation it "[a]utomatically becomes buffer-local when set";
;; therefore, `(setq-default case-fold-search nil)` won't do.
(setq-default case-fold-search nil)

I can still toggle the white-space and case-fold settings during a search, with M-s SPC and M-s c, respectively, but I never figured out how to change either of these settings only for the duration of the search.

Thanks to Constantine for the pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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