For any jump to a new buffer(for example from the ack buffer or godef-jump) point is left at the very bottom of the window. Is there a global configuration to automatically center occur and similar buffers without wrapping every conceivable function that jumps point very far away?

5 Answers 5


Use C-l or the command (function) bound to it, recenter-top-bottom. Hit it repeatedly up to 3 times, to cycle among different vertical positions for the cursor. See the full doc for the command for more possibilities:

C-l runs the command recenter-top-bottom (found in global-map), which is an interactive compiled Lisp function in window.el.

It is bound to C-l.

(recenter-top-bottom &optional ARG)

Move current buffer line to the specified window line. With no prefix argument, successive calls place point according to the cycling order defined by recenter-positions.

A prefix argument is handled like recenter: With numeric prefix ARG, move current line to window-line ARG. With plain C-u, move current line to window center.

  • 5
    While this technically answers the question, I think adding another keystroke to a common workflow is even worse than rebinding all of the jump functions I frequently use. I'll rephrase the question to be more specific.
    – Aaron Lee
    Commented Jul 30, 2015 at 11:36
  • @AaronLee There are two different cases -- where point goes when the buffer is created, and where point is when you move into an existing buffer. I think you probably want to change the first, but not the second.
    – zck
    Commented Jul 30, 2015 at 16:17
  • 1
    @zck Mostly, the Auto Scrolling setting apply to any case where point is moved outside of the buffer. Unfortunately those seem to scroll when I press "down" at the bottom of the page. So I guess there are three cases, new buffer, jumping within a buffer, and sequentially moving down or up lines. It seems to me like the first two should have similar functionality, but different from the third.
    – Aaron Lee
    Commented Jul 30, 2015 at 22:42
  • What do you mean by "jumping within a buffer"? Do you mean C-v, or using isearch, or something else? It would be helpful if you could give a set of instructions of how to reproduce what you see now, and what you want.
    – zck
    Commented Jul 31, 2015 at 3:41

There are two commands that can help out here. One is mentioned by Drew in a previous answer:

recenter-top-bottom, which is bound to C-l by default, keeps point at the same position in the text file, but scrolls the window with the text file to move that position to the center, top and then bottom of the file. This is often useful when point is near the bottom of a file, but want to see context around point.

But there's another function that is relevant here. M-r, or move-to-window-line-top-bottom, holds the text in the buffer where it is, but moves point to the middle, top, and bottom of the visible area. This is useful when you want to move point somewhere else in the visible area.


This recenters the point when the window moves by more than 5 lines, except in Isearch mode or when a scroll command is used (for scroll commands I think it's best to use scroll-preserve-screen-position, scroll-conservatively and hscroll-step).

(defun my-recenter-after-jump (window new-win-start)
  "Recenter the point after a non-scroll command brings it out of view.
This function is meant to be called from the hook ‘window-scroll-functions’."
  (with-selected-window window
    (let* ((new-start-line (line-number-at-pos new-win-start))
           (old-start-line (or (bound-and-true-p last-start-line-memo)
                               (line-number-at-pos (point))))
           (distance (abs (- old-start-line new-start-line))))
      (when (and (> distance 5)
                 (not isearch-mode)
                 (not (get last-command 'scroll-command)))
      (setq-local last-start-line-memo new-start-line))))
(add-hook 'window-scroll-functions #'my-recenter-after-jump)

Take my-horizontal-recenter from Trey Jackson's answer to "Command to center screen horizontally around cursor on emacs?"

(defun my-horizontal-recenter ()
  "make the point horizontally centered in the window"
  (let ((mid (/ (window-width) 2))
        (line-len (save-excursion (end-of-line) (current-column)))
        (cur (current-column)))
    (if (< mid cur)
        (set-window-hscroll (selected-window)
                            (- cur mid)))))

Finally, for Isearch I use

(defun scroll-recenter-setup ()
  "Set up scrolling so that the point is recentered if it would move off-screen.
This function is meant for ‘isearch-update-post-hook’. It makes Isearch recenter
a match if it is outside the current view."
  ;; Both variables are set to their default value here.
  (setq scroll-conservatively 0)
  (setq hscroll-step 0))

(defun my-scroll-setup ()
  "Apply my scroll settings.
This function is meant for ‘isearch-mode-end-hook’. It's used to restore my
settings after calling ‘scroll-recenter-setup’."
  (setq scroll-conservatively <your preference>)
  (setq hscroll-step <your preference>))

(add-hook 'isearch-update-post-hook #'scroll-recenter-setup)
(add-hook 'isearch-mode-end-hook #'my-scroll-setup)

This might be of help. I use them in dap debugging.

 (defun centreCursorLineOn() 
   "set properties to keep current line approx at centre of screen height. Useful for debugging."
   ;; a faster more concise alternative to MELPA's centered-cursor-mode
   (setq  scroll-preserve-screen-position_t scroll-preserve-screen-position scroll-conservatively_t
          scroll-conservatively maximum-scroll-margin_t maximum-scroll-margin scroll-margin_t
   (setq scroll-preserve-screen-position t scroll-conservatively 0 maximum-scroll-margin 0.5
         scroll-margin 99999))

 (defun centreCursorLineOff() 
   (setq  scroll-preserve-screen-position scroll-preserve-screen-position_t scroll-conservatively
          scroll-conservatively_t maximum-scroll-margin maximum-scroll-margin_t scroll-margin

I was most annoyed by this when choosing a result from helm, opening a file to a specific line taller than the window would open the file with the cursor at the very bottom of the window. I had to either C-l a few times to center or z z (spacemacs/evil recenter) etc.

This hook fixes it for helm actions:

  (add-hook 'helm-after-action-hook 'recenter)

I still miss the re-centering in non-helm cases. Like if I'm browsing a commit with magit and inspect a changed line in a file it will open the file to that line and the line will be at the bottom. Looking for a hook on that one still.

  • I don't think there's a hook that applies after a motion (cursor movement) of any kind. If there were, then you could use that. But there are some ...-after-jump-hooks.
    – Drew
    Commented Feb 18, 2022 at 18:37
  • 1
    I actually fixed a lot of my scrolling issues recently by setting scroll-conservatively to 0, and letting emacs Auto Scrolling take over, similar to one of the answers above.
    – sponrad
    Commented Feb 24, 2022 at 0:51

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.