For a few years now I've been trying to get emacs to alert me, somehow, to:
- lines longer than 80 characters
- literal tabs (
\t
) - carriage returns (
\r
) - trailing whitespace at the end of a line (
[ ]+$
)
I've tried many strategies, but usually the result was something I found a little too intrusive, or difficult to manage.
I've finally got something I like:
- Using whitespace-mode, characters after column 80 are in a light, easily readable purple.
- Using a customization to whitespace-mode, literal tabs are replaced by a noticeable right-pointing triangle symbol.
- Using custom font lock faces, the triangle literal tabs are made green.
- Using custom font lock faces, carriage returns are made blue.
- These settings are used in all modes, for all files.
Add this to your emacs initialization file (generally ~/.emacs
).
; make carriage returns blue and tabs green
(custom-set-faces
'(my-carriage-return-face ((((class color)) (:background "blue"))) t)
'(my-tab-face ((((class color)) (:background "green"))) t)
)
; add custom font locks to all buffers and all files
(add-hook
'font-lock-mode-hook
(function
(lambda ()
(setq
font-lock-keywords
(append
font-lock-keywords
'(
("\r" (0 'my-carriage-return-face t))
("\t" (0 'my-tab-face t))
))))))
; make characters after column 80 purple
(setq whitespace-style
(quote (face trailing tab-mark lines-tail)))
(add-hook 'find-file-hook 'whitespace-mode)
; transform literal tabs into a right-pointing triangle
(setq
whitespace-display-mappings ;http://ergoemacs.org/emacs/whitespace-mode.html
'(
(tab-mark 9 [9654 9] [92 9])
;others substitutions...
))
After installing the code, open this file in emacs.
- I'd like the literal tab triangles to be green, just as the characters after column 80 are purple. Currently it's the background of the triangles that are green, which I find a little jarring.
- It would be nice if you could have multiple "long line" standards. For example, one may tolerate 80 columns in some cases, but 120 in others. It would be neat if characters in column 80-120 were purple, and characters after column 120 were another color. whitespace-mode only supports one "long line" notion.
- Bug: trailing whitespace on long lines (lines longer than 80 columns) do not have their trailing whitespace marked red. Probably a conflict with the configuration that the non-space characters after column 80 get transformed to purple.
This is absolutely beautiful! Thank you.