Skip to content

Instantly share code, notes, and snippets.

@ymasory
Created September 27, 2012 15:44
Show Gist options
  • Save ymasory/3794723 to your computer and use it in GitHub Desktop.
Save ymasory/3794723 to your computer and use it in GitHub Desktop.
configuring emacs to show whitespace and long line problems

Configuring emacs for long line & whitespace detection

Goals

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.

Current solution

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.

Code

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...
   ))

Try it

After installing the code, open this file in emacs.

Future improvement

  • 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.
@xnandor
Copy link

xnandor commented Oct 25, 2014

This is absolutely beautiful! Thank you.

@oystersauce8
Copy link

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment