agentofuser
agentofuser

Reputation: 9307

Command to center screen horizontally around cursor on emacs?

I'm familiar with and use very frequently the C-l (recenter-top-bottom) to

Move current line to window center, top, and bottom, successively.

I'd like to have an equivalent command to move the current column to window center, left and right borders, successively. Either built-in or a snippet of Elisp.

Upvotes: 17

Views: 6346

Answers (2)

Trey Jackson
Trey Jackson

Reputation: 74430

Here you go:

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

And the obvious binding (from obvio171) is:

(global-set-key (kbd "C-S-l") 'my-horizontal-recenter)

Upvotes: 17

Pinochle
Pinochle

Reputation: 5563

If you move to a chosen column and hit C-x C-n, then the commands C-n and C-p will go to that column until you hit C-u C-x C-n to turn the behavior off.

A sort of poor-man's version of what you are looking for.

Upvotes: 4

Related Questions