8

Unlike isearch, evil-search mode ignores case-fold-search option.

Using evil-search like this for eg:

(setq-default evil-search-module 'evil-search)

Is it possible to make evil-search case sensitive?

ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • Looking at `evil-search` source, it looks like it follows `search-upper-case` instead – Felipe Lema Mar 10 '17 at 12:40
  • FTR, for making the `/` search case-sensitive, `case-fold-search` [_is_ the right option](https://emacs.stackexchange.com/questions/30570/how-do-i-do-case-sensitive-searches-using-evil-spacemacs?rq=1). – leftaroundabout Jan 27 '20 at 15:16
  • IIRC smart search overrides `case-fold-search` in this spesific case. – ideasman42 Jan 28 '20 at 05:12

2 Answers2

11

What to do

You can customize the variable evil-ex-search-case to tell it what you want evil to do when searching. Here's the docstring:

evil-ex-search-case is a variable defined in evil-vars.el. Its value is smart

Documentation:

The case behaviour of the search command. Smart case means that the pattern is case sensitive if and only if it contains an upper case letter, otherwise it is case insensitive.

You can customize this variable.

If you dig into the source code, this is the variable in question:

(defcustom evil-ex-search-case 'smart
  "The case behaviour of the search command.
Smart case means that the pattern is case sensitive if and only
if it contains an upper case letter, otherwise it is case
insensitive."
  :type '(radio (const :tag "Case sensitive." sensitive)
                (const :tag "Case insensitive." insensitive)
                (const :tag "Smart case." smart))
  :group 'evil)

Now: you can either use the customize interface to choose your option, or you could put the following in your init file:

(setq evil-ex-search-case 'sensitive)

Here's how I found this information.

First, I asked apropos to tell me about various functions and variables related to evil searches (M-x apropos evil search). I then searched that list for mentions of "case," and checked out the variable evil-ex-search-case. When that looked like the relevant variable, I followed the link in the help window to the source code to figure out what value I needed to set it to.

Dan
  • 32,584
  • 6
  • 98
  • 168
  • Thanks for the comprehensive answer, I'd actually read over the source code for evil-search but found multiple terms that mention case (`evil-ex-regex-case`. `evil-ex-regex-without-case`, `evil-ex-pattern-ignore-case`)... so wasn't sure which was important to access. – ideasman42 Mar 11 '17 at 06:25
  • This worked for me only after also going into emacs menu Options->Default Search Options, and unchecking "ignore case" – luser droog Sep 26 '20 at 01:28
2

Super old question, but for anyone looking for a more vim-like solution, try this:

(defun set-noic() 
  "set case sensitive" 
  (interactive) 
  (setq evil-ex-search-case 'sensitive))
(defun set-ic() 
  "set ignore case" 
  (interactive) 
  (setq evil-ex-search-case 'insensitive))

Then you can change case sensitivity using :set-ic or :set-noic, just like you did in vim. Just have to remember the dash.

farnsy
  • 161
  • 4
  • You just taught me that interactive functions could be called via the ex command line. I had no idea I could skip hitting **`M-x`** and just use **`:`** instead. Thank you. – g-gundam Oct 01 '22 at 01:26