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.

  • 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 1 '15 at 3: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 1 '15 at 4:57
  • @TomRegner How do you do 'flex' search in isearch? – Kaushal Modi Apr 1 '15 at 14:07
  • @kaushalmodi bitbucket.org/jpkotta/flex-isearch might do – Tom Regner Apr 10 '15 at 15:56
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

  • 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 1 '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 1 '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.

  • 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 5:40
  • What are C-w and M-e supposed to do? You can post at github.com/abo-abo/swiper/issues if you want me to add something. – abo-abo Apr 14 '15 at 8: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 6: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

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.