3

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?

7

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.

  • 1
    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 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 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 3:41
5

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.

0

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)
0

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.