In The Armchair

Emacs: reposition paragraph after justify

Posted in Computers by Armchair Guy on October 11, 2009

Emacs’ justify (fill-paragraph, usually bound to the key M-q) has one bit of behaviour that annoys me.  I prefer horizontal scrolling to breaking long lines.  If I type a long line that goes off the edge of the current emacs window, emacs will scroll left as expected.  If I then hit M-q, the paragraph gets justified.  So far so good.  But after justification the lines remain left-scrolled, so that I can’t see the beginning of the lines even if they are now small enough to fit in the emacs window.  I have to hit C-a C-e or something like that to re-scroll the text sideways.

The fix is simple: remap M-q to fill-paragraph, scroll-right instead of just fill-paragraph.  Scroll-right is smart and won’t scroll right if the window is so narrow that, even after fill-paragraph, the line doesn’t fit in the window.  This is exactly what I want.  I inserted the following code into my .emacs:

;; Automatically scroll-right after justification
(global-set-key (kbd “M-q”) (lambda () (interactive) (fill-paragraph nil) (scroll-right)))