5

During search and especially search-and-replace it would be nice to have the surrounding lines as context. But when jumping to the next hit, the cursor sometimes ends at the lowest visible line (or even worse at a corner).

I would like the text to follow the cursor during search. Is there a way to do this?


This question is similar to this one, but I don't want that all the time but only when jumping during search

  • 2
    helm-swoop has this feature and enables it by default. Take a look at its README.md (mainly GIF pictures). – xuchunyang Mar 26 '15 at 8:50
5

For Isearch, you can set option isearch-allow-scroll to non-nil. That lets you scroll the buffer you are searching, without exiting Isearch.

And note that "scrolling" here includes C-l, which recenters the text (repeat C-l to put the current search hit at the top, middle, bottom of the window).

5

The simplest way to do this is:

(defadvice isearch-update (before my-isearch-reposite activate)
  (sit-for 0)
  (recenter 1))

A better but more complicated:

(defadvice isearch-update (before my-isearch-update activate)
  (sit-for 0)
  (if (and
       ;; not the scrolling command
       (not (eq this-command 'isearch-other-control-char))
       ;; not the empty string
       (> (length isearch-string) 0)
       ;; not the first key (to lazy highlight all matches w/o recenter)
       (> (length isearch-cmds) 2)
       ;; the point in within the given window boundaries
       (let ((line (count-screen-lines (point) (window-start))))
         (or (> line (* (/ (window-height) 4) 3))
             (< line (* (/ (window-height) 9) 1)))))
      (let ((recenter-position 0.3))
        (recenter '(4)))))

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.