2

I have a specific problem that when I run "grep", my default grep command contains the -i option, and sometimes I wish to repeat the last search but case-sensitive. So I press M-p (previous command),

grep <some-options> -i lastSearchWord

then a number of M-b (backward-word) to get to the -i option, but this puts me between "-" and "i": so then I have to switch to C-b M-d to delete the option. And it's annoying to switch from Meta to Control here (and the need for this extra command at all): it would be more helpful to get the cursor to before the "-".

I know it's possible to redefine the categorization of characters per mode, but I am out of touch with Emacs terminology so don't know what to search for. How do I redefine "-" to be a "word character" in the Minibuffer only?

Drew
  • 77,472
  • 10
  • 114
  • 243
Alexander
  • 121
  • 1
  • If you use lgrep you can just customize option grep-template to be what you want, i.e., to not include -i. – Drew Apr 01 '22 at 17:32
  • The question title seems to be off-target, and you can probably come up with better tags... – Drew Apr 02 '22 at 15:53
  • The original question's title is How to define "-" as a word character in minibuffer? You cannot get better than that, and I hate it when people who think they're doing something for the improvement actually make stuff worse. Such a waste. – Alexander Apr 04 '22 at 07:14
  • I rolled back @ideasman42's edit. – Drew Apr 04 '22 at 15:31
  • The reason to make this question more general is the delimiter character isn't important, someone else may ask "How to define "." as a word character in minibuffer in macOS?" and "How to define "?" as a word character in minibuffer for the German language?" - there is no need to be spesific about which delimiter exactly you want to change. That you want to change a delimiter is enough information. – ideasman42 Apr 04 '22 at 21:20

2 Answers2

1

On my system, backward-kill-sexp (which I have on C-M-backspace) is the thing to execute after backward-word. That eliminates both the "i" and its preceding "-".

If you need to do this often, you might consider writing your own command:

(defun my-case-insensitive-grep ()
  "Call `grep' without the "-i" switch."
  (interactive)
  (grep (s-replace " -i " " " (grep-default-command))))
Phil Hudson
  • 1,741
  • 10
  • 13
0

Starting with Emacs 28.1, this can be done much like any other mode using a mode hook & modify-syntax-entry.

(add-hook 'minibuffer-mode-hook
  (lambda ()
    ;; Treat '-' as a word.
    (modify-syntax-entry ?- "w")))
ideasman42
  • 8,786
  • 1
  • 32
  • 114
  • 2
    Does minibuffer-mode-hook really exist? Seems to not be defined for me (26.1) – JeanPierre Apr 01 '22 at 11:42
  • Yes, it does in emacs 29 (and probably in 28 as well: I haven't checked). It does not exist in 27.2 AFAICT and presumably not in anything earlier either. – NickD Apr 01 '22 at 12:54
  • It went in to 28.1 with this commit: "commit 55db25b2579e5d84dfcf3a15957fc7a827abf25f Author: Alan Mackenzie [email protected] Date: Tue Apr 20 10:14:40 2021 +0000
    Introduce and use minibuffer-mode.  This fixes bug #47150
    

    ...`

    – NickD Apr 01 '22 at 12:57
  • This is a future answer, presumably. Neither Emacs 28 nor Emacs 29 have been released. The latest release is 27.2. – Drew Apr 01 '22 at 17:27
  • Emacs 28.1 has now been released. – ideasman42 Apr 04 '22 at 21:17