28

In Vim I can :set wrapscan so that when I do an incremental search, the cursor jumps to the first match whether the first match is above or below the cursor.

In Emacs, if I start a search via C-s, the search fails saying Failing I-search if the first match is above the cursor. If I hit C-s again it then wraps the search, saying Wrapped I-search. How do I wrap and jump the cursor by default as in Vim, without having to C-s a second time?

4 Answers 4

20

The easiest way to do this is to use the following defadvice:

(defadvice isearch-repeat (after isearch-no-fail activate)
  (unless isearch-success
    (ad-disable-advice 'isearch-repeat 'after 'isearch-no-fail)
    (ad-activate 'isearch-repeat)
    (isearch-repeat (if isearch-forward 'forward))
    (ad-enable-advice 'isearch-repeat 'after 'isearch-no-fail)
    (ad-activate 'isearch-repeat)))

When Isearch fails, it immediately tries again with wrapping. Note that it is important to temporarily disable this defadvice to prevent an infinite loop when there are no matches.

7
  • Thanks, I see how this is supposed to work, but it doesn't seem to do anything. The function isearch-no-fail doesn't exist in my Emacs. I grepped all the Emacs source code and it's not in there. Nov 13, 2008 at 19:20
  • isearch-no-fail is just a name of this defadvice, you can change it to anything you want. I tested this code in GNU Emacs 22 and GNU Emacs 23, and it works exactly as you described. Could you provide more details to understand why it doesn't work for you?
    – link0ff
    Nov 13, 2008 at 21:04
  • Never mind, I don't know where my mind was. It does work, but it was slightly different than what I wanted. I wanted it to wrap the search as I typed, without hitting C-s again at all. Thanks! Nov 13, 2008 at 21:45
  • 2
    This solution has the annoying side-effect of requiring you to hit backspace twice to remove the first character that fails to match.
    – Hinrik
    Oct 1, 2015 at 9:59
  • A bit new to emacs here. How do you execute this function in a keymap? I tried (global-set-key (kbd "C-/") 'isearch-no-fail) but I get 'Wrong type argument' in mini-buffer.
    – vexe
    Dec 19, 2015 at 0:04
8

Jurta's answer got most of the way there. This is the wanted behavior:

;; Prevents issue where you have to press backspace twice when
;; trying to remove the first character that fails a search
(define-key isearch-mode-map [remap isearch-delete-char] 'isearch-del-char)

(defadvice isearch-search (after isearch-no-fail activate)
  (unless isearch-success
    (ad-disable-advice 'isearch-search 'after 'isearch-no-fail)
    (ad-activate 'isearch-search)
    (isearch-repeat (if isearch-forward 'forward))
    (ad-enable-advice 'isearch-search 'after 'isearch-no-fail)
    (ad-activate 'isearch-search)))
3

Set isearch-wrap-pause to no.

https://www.gnu.org/software/emacs/manual/html_node/emacs/Repeat-Isearch.html

3
0

I found this solution to be perfect for me (a former Vim user). It makes isearch work just like Vim. It enables automatic wrap-around as well as switching search direction with just one keypress of C-s / C-r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.