Changing the Recentering Order

I was trolling through some of abo-abo’s old posts over at (or emacs and came upon this gem concerning the order of positions for recenter-top-bottom. I use this command all the time; it’s really handy when a search result leaves the target at the bottom of the screen. A simple Ctrl+l and the line is repositioned to the middle of the screen. Another Ctrl+l and it’s positioned at the top. A third invocation returns it to the bottom.

As abo-abo says, this is a bit counter intuitive and not as handy as you’d like it to be. In the common case of searching for a function, you’d like to position it at the top so as much of the function as possible is visible. Of course you can do that by pressing Ctrl+l twice but it would make more sense for it to start at the top.

Being Emacs, the behavior is, of course, completely configurable. You can set the order to be anything you like. If, as abo-abo says, you believe in gravity and want as much of the result showing as possible, you can set the order to be top-middle-bottom with

(setq recenter-positions '(top middle bottom))

I like that better but others may disagree. The beauty of Emacs, as always, is that you can have it your way.

This entry was posted in General and tagged . Bookmark the permalink.
  • glucas

    You might also like `reposition-window` which I believe is bound to `C-M-l` by default. This command "makes the current definition and/or comment visible."

    • jcs

      Yup. I use that too but I often forget and just use recenter-top-bottom. reposition-window is a little smarter about displaying functions so I probably should make an effort to use it more.

      • Tangent: `balance-windows' for when you mucked about with your windows and want to rebalance them.

  • My mission is to wrap every travel function that leaves the cursor at the bottom of the screen. The only time I don't want to see what is below is when i ace-jump to something (or avy goto). For example navigating org source blocks leave them un-visible forcing control-i. It is on the todo list.

    • jcs

      Right. What's up with leaving, say, a defun at the bottom of the screen omitting the rest of the function? I'm sure folks can come up with a use case where it makes sense but it always annoys me.

      • (defun help/recenter-line-near-top-fn ()
        "Move current line near top"
        (interactive)
        (let ((recenter-positions '(5)))
        (recenter-top-bottom)))
        (add-hook 'occur-mode-find-occurrence-hook #'help/recenter-line-near-top-fn)