7

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

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

3 Answers 3

7

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).

7

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)))))
1

The smooth-scrolling package at aspiers/smooth-scrolling solves this nicely. The following code initialises the package.

(use-package smooth-scrolling
  :ensure t
  :custom (smooth-scroll-margin 6)
  :init (smooth-scrolling-mode))

Your Answer

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

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