5

In programming files, I use whitespace-mode to highlight the tab and long lines. The default highlighting is too garnish for me.I just want to highlight them with a gray background and keep whatever normal color it should be for the font. How could I set that?

The following setup does not work. I'd like the code beyond 80 columns appearing yellowish, as the characters inside 80 columns in the snapshot.

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

;; face for Tabs
(set-face-attribute 'whitespace-tab nil
                    :background "#555"
                    :weight 'bold)

whitespace-mode

RNA
  • 146,987
  • 15
  • 52
  • 70
  • Same problem. In default theme, trailing whitespace is a nice grey. In every other color theme, it blazes red as if some fatal error. The set-face-attribute answer below has no effect. – Dave Cohen Jul 10 '13 at 16:54

2 Answers2

4

set-face-attribute changes only the attributes you specify.

Set :foreground to nil:

(set-face-attribute 'whitespace-line nil
                    :foreground nil
                    :background "#555"
                    :weight 'bold)
Dmitry
  • 3,625
  • 2
  • 30
  • 28
  • I know, but nil will set the font white not yellow. – RNA Feb 01 '13 at 03:39
  • I see. Short answer: you can't do it. Longer answer: you'd need to patch whitespace.el: https://gist.github.com/72d39c507d56c5c5e0ed – Dmitry Feb 01 '13 at 04:05
  • I am looking for something similar to defadvice as for function – RNA Feb 01 '13 at 04:09
  • You could get this by advising both `whitespace-color-on` and `font-lock-add-keywords`, setting some var to to in the former, checking its value in the latter and forcibly setting the `override` value in the `keywords` spec to `prepend`, but really, it's a horrible solution. – Dmitry Feb 01 '13 at 04:12
  • Send a feature request to the Emacs bug tracker, I'm sure we can get this change into trunk in no time. Using `prepend` there makes sense. – Dmitry Feb 01 '13 at 04:15
  • with the command in this answer, i get an error:::: error: Invalid face, whitespace-line –  Jan 04 '15 at 06:52
  • @MadhavanKumar Try `(require 'whitespace)` first. – Dmitry Jan 04 '15 at 09:59
3

For me the unpleasant color turned out to be trailing-whitespace and I'm using this:

;; whitepace looks rediculous in color themes.
(defadvice color-theme-install (after my-color-theme-install-after activate)
  "Fix trailing-whitespace after color theme destroys it"
  (set-face-attribute 'trailing-whitespace nil
                      :foreground 'unspecified
                      :inverse-video 'unspecified
                      :slant 'unspecified
                      :weight 'unspecified
                      :background "#fff"))
Dave Cohen
  • 1,277
  • 2
  • 15
  • 21
  • Doing this for both trailing-whitespace and whitespace-line worked for me with emacs 24.4 -- except that I used (add-hook 'python-mode-hook '(lambda () (set-face-attribute ...))). No doubt that's not the best place, but worked for my purpose. – Croad Langshan Mar 14 '15 at 22:38