abo-abo / swiper

Ivy - a generic completion frontend for Emacs, Swiper - isearch with an overview, and more. Oh, man!
https://oremacs.com/swiper/
2.31k stars 338 forks source link

reverse i-search (C-r): select+submit with a single Return keypress? #847

Open ivanbrennan opened 7 years ago

ivanbrennan commented 7 years ago

The manual describes ivy-reverse-i-search saying,

This works just like C-r at the bash command prompt...

but in bash, typing return from within reverse-i-search will select and execute the current match, rather than simply inserting it on the command line and waiting for an other return to run it.

I've always preferred this behavior to the way Emac's handles minibuffer i-search, because 99% of the time I want to execute the match without further editing. I've been using the following to adjust Emacs' behavior accordingly:

(defun ivan/isearch-exit ()
  "Run isearch-exit, and if in the minibuffer, submit the search result as input."
  (interactive)
  (isearch-exit)
  (if (minibuffer-window-active-p (selected-window))
      (minibuffer-complete-and-exit)))

(with-eval-after-load "isearch"
  (define-key isearch-mode-map [remap isearch-exit] #'ivan/isearch-exit))

When I actually do want to edit the match before executing it, any horizontal cursor movement (e.g. C-a) will exit the search, leaving the match on the command-line/minibuffer without executing it.

I'd like to make ivy-reverse-i-search behave the same, but I haven't found a way to do so. Is it possible?

abo-abo commented 7 years ago

This works just like C-r at the bash command prompt...

Yes, it should say /almost/ like. I use C-r for swiper very often, and it really makes sense to continue editing there.

In other cases as well: usually after C-r is done, there are more than 1 candidate. Simply going with the first of 10 available candidates would be quite inflexible and frustrating.

I'd like to make ivy-reverse-i-search behave the same, but I haven't found a way to do so. Is it possible?

You can redefine ivy-reverse-i-search, it's very small. Replacing ivy--cd-maybe with ivy--done should do the trick.

ivanbrennan commented 7 years ago

Thanks, @abo-abo. Replacing (ivy--cd-maybe) with (ivy-done) works. I still need to find a way to allow further editing in the rarer cases where that's what I want.

Though using horizontal movements to select the match without executing it works outside of ivy, it's a bit of a hack. I'm going to try and bind the original behavior to shiftreturn.

That way, I could use: