When selecting an item from the occur buffer, the highlighted line is at the very bottom of the visible screen. This is less useful than having it 25-50% of the way down the screen(so it's easy to read the text around the found line). Is this possible?

1 Answer 1


I don't see the behavior that you describe, at all. For me, when I choose an occurrence in buffer *Occur* it visits the occurrence in the source buffer, putting that smack in the middle of the source-buffer window (vertically).

But if you see what you describe, then just put some window-recentering code on occur-mode-find-occurrence-hook.

Something like this. You can use any comparison condition you like. Here, I used "within 10 lines of the top or bottom", but you could use percentages etc.

(defun foo ()
  (let ((line   (line-number-at-pos)))
    (cond ((<= line (+ (line-number-at-pos (window-start)) 10))
           (recenter 10))
          ((>= line (- (line-number-at-pos (window-end)) 10))
           (recenter -10)))))

(add-hook 'occur-mode-find-occurrence-hook 'foo)
  • 1
    Yup, that'll do it. I get OP's behavior, FWIW.
    – Dan
    Nov 17, 2014 at 2:50
  • Strange that I do not, then. emacs -Q on MS Windows 7. Any hits I click (or use RET on) in the occur buffer are shown centered vertically in the source buffer.
    – Drew
    Nov 17, 2014 at 2:51
  • You're right, emacs -Q (on Linux) gives the behavior you describe. Something must be buried somewhere is the setup.
    – Dan
    Nov 17, 2014 at 2:53

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.