7

When performing search/isearch in a text, sometimes it would be desirable to see a few lines after the highlighted searched string, it other words to avoid that the highlighted word to be at the bottom of the window.

For example if we have the following text in a buffer, try to search the last word that you are able to see. For me it is the word principles (as I have maximized the frame). The highlighted word (in my situation the word principles) is at the bottom of the screen. I would like to see always few lines (if possible) after the word principles after searching.

Surrounded
occasional 
pianoforte 
alteration 
unaffected 
impossible 
Perceived 
knowledge 
certainly 
Although 
moreover 
mistaken 
kindness 
horrible 
distance 
marriage 
country 
demesne 
Bachelor 
domestic 
extended 
doubtful 
Greatly 
Improved 
provided 
Dwelling 
speedily 
ignorant 
steepest 
Admiration 
instrument 
affronting 
invitation 
reasonably 
ecstatic 
wondered 
disposal 
curiosity  
shameless 
explained.
sincerity 
extremity 
additions 
principles
Carriage 
Remainder 
household 
direction 
zealously 
Sufficient 
motionless
compliment 
stimulated 
assistance
2
  • You could write a function that scrolls some lines after every hit, and add that when the search hit key is called.
    – ReneFroger
    Jul 5, 2015 at 17:08
  • @ReneFrogertjuh Normally, if at any moment, one cancels search query, one should returns to the state of buffer before performing search.
    – Name
    Jul 5, 2015 at 17:37

2 Answers 2

5
(setq scroll-margin 3)

Works everywhere, not just during isearch.

scroll-margin. Default: 0. Number of lines of margin at the top and bottom of a window. Recenter the window whenever point gets within this many lines of the top or bottom of the window.

1
  • Thanks for this simple solution which answers the question. By the way, let me ask a question. What is the role of 3 in this answer? If I replace 3 with 1, I get the same thing.
    – Name
    Jul 6, 2015 at 17:10
2

M-x apropos-variable RET isearch.*\(hook\|function\) RET leads you to isearch-update-post-hook, which seems to be what you're looking for. So ...

(add-hook 'isearch-update-post-hook
          (lambda ()
            (when (> (length isearch-string) 0)
              ;;or whatever
              (recenter)))
          nil t)
1
  • 1
    I tested your code, it seems fine, expect if the searched word is already visible before searching. If I replace (when (> (length isearch-string) 0)... in your code by (when (> 1 0)... it seems to work. But I do not understand from where the problem arises.
    – Name
    Jul 6, 2015 at 1:09

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.