Keep track of whitespaces and column 80 overflow

by Ruslan Spivak on September 27, 2010

When working in Emacs python mode I keep track of whitespaces and column 80 overflow using a whitespace-mode that is part of Emacs 22+

Here is a snippet from my .emacs:

;; nuke trailing whitespaces when writing to a file
(add-hook 'write-file-hooks 'delete-trailing-whitespace)

;; display only tails of lines longer than 80 columns, tabs and
;; trailing whitespaces
(setq whitespace-line-column 80
      whitespace-style '(tabs trailing lines-tail))

;; face for long lines' tails
(set-face-attribute 'whitespace-line nil
                    :background "red1"
                    :foreground "yellow"
                    :weight 'bold)

;; face for Tabs
(set-face-attribute 'whitespace-tab nil
                    :background "red1"
                    :foreground "yellow"
                    :weight 'bold)

;; activate minor whitespace mode when in python mode
(add-hook 'python-mode-hook 'whitespace-mode)

Because my Emacs is setup to save/restore desktop I’ve added the following lines to play nicely with the whitespace-mode when restoring a previous session:

;; save whitespace-mode variables
(add-to-list 'desktop-globals-to-save 'whitespace-line-column)
(add-to-list 'desktop-globals-to-save 'whitespace-style)

This is how trailing whitespaces, tabs and long lines tails look in my Emacs buffer:

import sys

def main():
    """This line is longer than 80 cloumns ............................."""
    values = [
        # next line contains leading tab

if __name__ == '__main__':
    main() # this line contains trailing whitespaces    

UPDATE November 15th, 2011
In Emacs 24 you have to add a face specifier to the whitespace-style to make trailing whitespaces highlighted:

(setq whitespace-line-column 80
      whitespace-style '(face tabs trailing lines-tail))
If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 12 comments… read them below or add one }

Marius Gedminas September 28, 2010 at 9:14 PM

Ironically we can’t see how long lines look in your editor because your blog uses a narrow fixed-width layout that truncates it.

What’s wrong with simple screenshots anyway?


Ruslan Spivak September 28, 2010 at 9:51 PM

Hey Marius,

Thanks for the comment.

If you look at the post then you should be able to see that a ‘main’ function’s doc string
“”"This line is longer than 80 cloumns ….”"” has a red marker at the end denoting 80 column overflow.

Screenshots are fine. It’s just I have a convenient keybinding in Emacs to htmlize a region in a form suitable for Wordpress.
But I guess I’ll have to create a keybinding to capture screenshots of regions or to change my blog’s theme :)


Valeriy Zamarayev September 30, 2010 at 1:22 AM

That’s nice, I’ll use it when I want to be very strict about this.

I have a simpler setup: I use column-marker to mark the 79′th column, so I have a small warning on the lines that exceed 80 chars. Plus, I put whitespace-cleanup on a save hook for all my projects. That’s both easy and not distracting, it doesn’t bug me when I read some else’s code, which sometimes is full of long lines, and spacing is done right automatically where I need it.


Ruslan Spivak September 30, 2010 at 9:31 AM

Hi Valeriy,

That’s why I love Emacs – you can tweak it in a way that works best for you :)

The reason why I switched completely to whitespace-mode is that it’s now part of Emacs and gives me exactly what I need.

Thanks for reminding about whitespace-cleanup. I’ve added it to ‘before-save-hook to replace (add-hook ‘write-file-hooks ‘delete-trailing-whitespace)


Adam Connor October 11, 2010 at 10:39 AM

Works for me except when in Python mode (i.e., if I change a Python buffer to Text mode, I see it). Any suggestions?


Ruslan Spivak October 11, 2010 at 6:08 PM

Hi Adam,

It’s hard to tell. When in Python mode can you see ‘ws’ alongside ‘Python’ in your mode line?

Try to turn whitespace-mode on/off manually with M-x whitespace-mode when in Python mode to see if it helps.


Adam Connor October 12, 2010 at 7:39 PM

Thanks, I’ve tried that, but no luck. Guess I need to do more research on how major and minor modes conflict.


Ashton Kemerling November 2, 2012 at 1:45 PM

Adam: I had a similar issue lately. Add a white-space call to my python-mode-hook caused whitespace mode to look weird and not work properly with python-mode. The following line appears to have resolved this issue though:

(setq whitespace-style ‘(face tabs trailing lines-tail))


zech July 16, 2012 at 1:11 PM

This is neat. But I have a problem with you settings. I use emacs 24. Even I add “face” to “whitespace-style”. I still have problems of “error: Invalid face, whitespace-line” and “error: Invalid face, whitespace-tab”. Do you have any idea? Thanks


Ruslan Spivak July 16, 2012 at 8:02 PM

The above setup works for me perfectly fine in Emacs 24. The only thing I could think of would be that you somehow don’t have a whitespace.el file available as part of your Emacs. Try M-x whitespace-mode and see if it works at all or you get any kind of error.


spiderbit October 22, 2012 at 5:30 PM

have the same error here, and whitespace mode is there. Using newest Ubuntu 12.10 emacs version…


Alexey Fedotov January 21, 2015 at 2:20 PM

Thanks for the settings snippet, Ruslan!
Zech: I’m not sure if this concerns you still but just in case if others face the same problem. (i.e. “error: invalid face, whitespace-line”). I resolved the issue by requiring the “whitespace” package first by writing “(require ‘whitespace)” before any whitespace style modifications.


Speak your mind

{ 2 trackbacks }

Previous post:

Next post: