2016/02/11: Different line lengths for different file types (in emacs)

Being aware of too long lines when coding is useful and that's why I use the whitespace mode in emacs. However, now I got at the point, that 80 characters is no longer the gold standard for all types of files, so I want the value when a line is considered long to vary by major mode. This means that the desired line length has to be stored in a buffer-local variable. A natural choice here is fill-column; it automatically becomes buffer-local when set and the white space mode supports reflecting over that variable. To set the global default, it can be custom set (I set it to 80) and then use hooks the major-mode hooks to override for buffer types with different desired lengths.

The relevant part of my .emacs looks as follows.


(require 'whitespace)
(setq whitespace-style '(face lines-tail trailing tabs empty))
(global-whitespace-mode t)
(setq whitespace-line-column nil)
(add-hook 'dired-mode-hook '(lambda () (setq fill-column 1024)))
(add-hook 'java-mode-hook '(lambda () (setq fill-column 100)))


(custom-set-variables
 ;; ...
 '(fill-column 80)
 ;; ...
)
download