8

I frequently mistype "_" as "-" when doing a quick isearch-forward.

E.g. I'm looking for snake_case and accidentally type C-s snake-case and get no results. Best case I notice quickly enough and don't have to delete too much of the search term, worst case I basically have to type it over again.

Is there some way to hook into isearch-forward to change my query into a isearch-forward-regexp like snake[-_]case?

I need to actually search for lisp-case often enough that simply changing "-" to "_" is going to cause more problems than it solves.

Tom
  • 181
  • 3
  • 6
    You can avoid typing the whole thing again. Do `M-e` to jump to the mini-buffer to edit the search string while isearch is active. Then `C-e M-b BKSPC _` and `C-s` to continue search. – Kaushal Modi Apr 01 '15 at 03:54
  • By using a more modern, fuzzy matching search 'engine', you simply type 'sc' and snake-case, SnakeCase and snake_case all match (use ido together with flx or somesuch for example) – Tom Regner Apr 01 '15 at 04:57
  • @TomRegner How do you do 'flex' search in `isearch`? – Kaushal Modi Apr 01 '15 at 14:07
  • @kaushalmodi bitbucket.org/jpkotta/flex-isearch might do – Tom Regner Apr 10 '15 at 15:56

2 Answers2

8

I can't imagine any such automated behaviour not then causing you pain when that's not what you wanted it to do.

To my mind kaushalmodi's recommendation of M-e is the best answer -- making it easy to correct the problem seems preferable to trying to make isearch read your mind.

Note that isearch is smart enough to take note of the first character where the search failed, and M-e places the cursor at that position automatically so, even when you've continued to type subsequent characters, the typical key sequence to fix the pattern and resume searching would just be:

M-eC-d_C-s

phils
  • 48,657
  • 3
  • 76
  • 115
  • That's awesome! TIL: *isearch is smart enough to take note of the first character where the search failed, and `M-e` places the cursor at that position automatically* – Kaushal Modi Apr 01 '15 at 15:36
  • I didn't know about `M-e`, that certainly makes things easier, especially since it jumps back to the typo. – Tom Apr 01 '15 at 22:30
5

If you use swiper and search for snake case, it will match both snake_case and snake-case.

swiper is a new isearch alternative that gives you an overview in the minibuffer as you search.

swiper.png

In the image above, the input is ivy m that translates to the regex ivy.*m. There are 36 matches for this regex. You can navigate between them with C-s/C-r or C-n/ C-p.

You can install swiper with M-x package-install. It's available in both MELPA and GNU ELPA.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • swiper (and swiper-helm) are neat. I've added them to my setup and am trying them out for a few days. Not quite what I was after though as I'll have to type something different than what I'm searching for, will see if I get used to that. Also, any idea how to get C-w and M-e work while in swiper mode? – Tom Apr 14 '15 at 05:40
  • What are `C-w` and `M-e` supposed to do? You can post at https://github.com/abo-abo/swiper/issues if you want me to add something. – abo-abo Apr 14 '15 at 08:47
  • M-e is described in the comments and other answer - it edits the search in the minibuffer. C-w expands the search term with the symbol under the cursor. – Tom Apr 14 '15 at 23:28
  • OK, you don't need `M-e` for swiper, it's already in `M-e`. And to get the thing at point, press `M-n`. There's no way to expand it yet, but you can just continue typing. – abo-abo Apr 15 '15 at 06:24
  • M-n is what I was after, that's good to know. The useful feature M-e is that it moves the point to the first non-matching character, I don't know if that's practical with the fuzzy matching though. – Tom Apr 16 '15 at 23:12
  • The way swiper works, is that you start out in `M-e`. So any time you mistype, you can simply use `DEL` or `M-DEL`. Both your buffer and the minibuffer try to keep you informed when you've mis-typed. – abo-abo Apr 17 '15 at 08:22
  • I understand that, but it just gets back to the original question then: I'm typing fast enough that by the time I realize I've mis-typed I've got to DEL multiple characters. M-DEL almost works in this example: "snake-case" doesn't match, M-DEL goes back to "snake-" so I still have to DEL to get back to "snake" and then type "_case" again. – Tom Apr 18 '15 at 10:32
  • Doesn't sound like a big deal. Just re-define `M-DEL` to delete the separator if it's so important for you. It's trivial to do. – abo-abo Apr 18 '15 at 15:19