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
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 commandrecenter-top-bottom
(found inglobal-map
), which is an interactive compiled Lisp function inwindow.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 prefixARG
, move current line to window-lineARG
. With plainC-u
, move current line to window center.

- 75,699
- 9
- 109
- 225
-
5While 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 Jul 30 '15 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 Jul 30 '15 at 16:17
-
@zck Mostly, the [Auto Scrolling](http://www.gnu.org/software/emacs/manual/html_node/emacs/Auto-Scrolling.html#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 Jul 30 '15 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 Jul 31 '15 at 03: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.

- 8,984
- 2
- 31
- 65
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’."
(interactive)
(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)))
(recenter)
(my-horizontal-recenter))
(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"
(interactive)
(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."
(interactive)
;; 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’."
(interactive)
(setq scroll-conservatively <your preference>)
(setq hscroll-step <your preference>))
(my-scroll-setup)
(add-hook 'isearch-update-post-hook #'scroll-recenter-setup)
(add-hook 'isearch-mode-end-hook #'my-scroll-setup)

- 1,525
- 9
- 22
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
(interactive)
(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
scroll-margin)
(setq scroll-preserve-screen-position t scroll-conservatively 0 maximum-scroll-margin 0.5
scroll-margin 99999))
(defun centreCursorLineOff()
(interactive)
(setq scroll-preserve-screen-position scroll-preserve-screen-position_t scroll-conservatively
scroll-conservatively_t maximum-scroll-margin maximum-scroll-margin_t scroll-margin
scroll-margin_t))

- 848
- 4
- 9
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.

- 101
- 1
-
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-hook`s. – Drew Feb 18 '22 at 18:37
-
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 Feb 24 '22 at 00:51