2

I’m trying to do a case insensitive search with M-x find-grep-dired. When I enter M-x find-grep-dired and hit ENTER and enter the directory name I get the prompt find-grep (grep regexp):. I understand this as “enter grep and then regexp”. So, for grep I enter -i for case insensitive search, and for regexp I enter the search word. But this does not work. What am I doing wrong?

enter image description here

zeynel
  • 371
  • 1
  • 11

1 Answers1

1

The second prompt is asking you for just a "grep regexp", i.e. a regexp conforming to grep syntax [1]. If you look at the implementation of find-grep-dired (C-h f find-grep-dired and click on the file link), you'll see that it uses find-grep-options, so you can add -i to the value of that variable. You can do that by asking for help on the variable (C-h v find-grep-options) and then clicking on the Customize link.

EDIT: if you don't want to change the value of find-grep-dired permanently, you can use the standard lisp facility of binding a variable temporarily with let:

(let ((find-grep-options "-q -i"))
   (call-interactively #'find-grep-dired))

That's a very general mechanism that is worth remembering and getting familiar with.

And you can of course package that in a command and bind it to a key if you want:

(defun find-grep-dired-case-insensitive ()
   (interactive)
   (let ((find-grep-options "-q -i"))
      (call-interactively #'find-grep-dired)))

(define-key global-map (kbd "C-c z") #'find-grep-dired-case-insensitive)

Footnote:

[1] There are other regexp syntaxes: e.g. perl regexp syntax is different in many ways from that of (basic) grep; GNU grep can use an extended syntax if you give it a -E option; Emacs has its own syntax for regexps. They are all similar in many ways, but differ in others. That's why the documentation of the function says to use a "grep regexp".

NickD
  • 29,717
  • 3
  • 27
  • 44
  • Thanks. I don’t see a file link to click. But I did, C-h v find-grep-options and on that screen I clicked “customize” and I got a screen with “Find Grep Options: -q". Do I need to change -q to -i? What does -q mean? I searched for “grep options -q” but nothing relevant resulted. – zeynel Feb 20 '22 at 13:29
  • I found that -q means "Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option." – zeynel Feb 20 '22 at 13:38
  • Correct: it's a grep option so you'll see it with man grep from a shell prompt. The documentation string of find-grep-options also shows this. Customize the variable to add -i to it (as I mention in the answer). – NickD Feb 20 '22 at 13:46
  • Ok. I added -i so now I have -q and -i, and saved and restarted emacs. But it does not work. The text I search has a capital letter "Abcd", so when I search for Abcd it finds it. But when I search for -i abcd it does not find it. What am I doing wrong? – zeynel Feb 20 '22 at 14:04
  • I added a screen shot showing that options changed to -q and -i – zeynel Feb 20 '22 at 14:11
  • What do you mean by "when I search for -i abcd"? Are you typing the - at the regexp prompt? If so, don't do that: just type abcd. – NickD Feb 20 '22 at 14:45
  • Ok, I got it. Now it works. Thanks. So is it better to use the terminal to search with grep? In the terminal, I can specify for each search if I want case sensitivity or not. In emacs, now all search is case insensitive, correct? – zeynel Feb 20 '22 at 14:57
  • Can you also explain regexp conforming to grep syntax? In the prompt I enter only regex, correct? – zeynel Feb 20 '22 at 15:02
  • Modified the answer to add some explanation of the points you asked about. – NickD Feb 20 '22 at 16:51
  • 1
    "I don’t see a file link to click." means that you have an incomplete installation of Emacs (you are missing the .el files for the elisp sources). My guess is that you're using a Debian-packaged emacs and need to apt-get install emacs-el (or something similar). – phils Feb 20 '22 at 21:00
  • ...and if that's the case then you're almost certainly also missing the should-be-built-in documentation. You should have several manuals available via the Help menu. If these aren't working, try apt-get install emacs-common-non-dfsg (or look up the correct name for that package for your particular OS/version). – phils Feb 22 '22 at 01:20