Thursday, June 16, 2011

Enable horizontal scrolling in Emacs

When dealing with lines longer than the window width, the default behavior of Emacs is to wrap lines. It can be useful to disable this feature. Add the following to your .emacs file to truncate long lines and display only the part of the line that is in the window. The conditional makes this work in Emacs 23 as well as older versions of Emacs.

;; horizontal scrolling
(if (boundp 'truncate-lines)
    (setq-default truncate-lines t) ; always truncate
  (progn
    (hscroll-global-mode t)
    (setq hscroll-margin 1)
    (setq auto-hscroll-mode 1)
    (setq automatic-hscrolling t)
   ))

5 comments:

Charles "Chuck" Hoffman said...

How might I modify this if I wanted it only in certain buffers, via a mode-hook?

tsengf said...

For example, to enable this feature in c-mode, wrap the original code with the following.

(add-hook 'c-mode-common-hook
'(lambda ()

< original code here >

))

Charles "Chuck" Hoffman said...

That turns it in just those buffers, then, not globally?

Charles "Chuck" Hoffman said...

... or should I also change the setq-default to just setq?

tsengf said...

Charles, you're absolutely right. Change setq-default to setq. Here is the complete example.

(add-hook 'c-mode-common-hook
'(lambda ()
(if (boundp 'truncate-lines)
(setq truncate-lines t)
)))