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

Beginner
  • 2,661
  • 3
  • 17
  • 25
  • 3
    [helm-swoop](https://github.com/ShingoFukuyama/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 08:50

3 Answers3

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

Drew
  • 75,699
  • 9
  • 109
  • 225
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)))))
link0ff
  • 1,081
  • 5
  • 14
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))
ppareit
  • 141
  • 4