views:

300

answers:

2

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.

+1  A: 

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.

Pinochle
This only limits the column which the cursor can be at, but doesn't move the text horizontally. What I want is for the column the cursor is at to be "dragged" to the center of the screen, doing a horizontal scroll.
obvio171
Thus the "poor-man's version" qualification.
Pinochle
+6  A: 

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)
Trey Jackson
This is perfect! Bound it to C-S-l, works like a charm! Thanks a lot! :)
obvio171