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?
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-lruns the command
global-map), which is an interactive compiled Lisp function in
It is bound to
(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
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.
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.
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
(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)
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)
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))