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 'qwerty', 'test', ] 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))



{ 11 comments… read them below or add one }
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?
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
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.
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)
Works for me except when in Python mode (i.e., if I change a Python buffer to Text mode, I see it). Any suggestions?
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.
Thanks, I’ve tried that, but no luck. Guess I need to do more research on how major and minor modes conflict.
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))
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
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.
have the same error here, and whitespace mode is there. Using newest Ubuntu 12.10 emacs version…
{ 2 trackbacks }