13

Right now I'm using:

(setq show-trailing-whitespace t)

In my .emacs to show trailing whitespace for my CC mode. I can't seem to figure out how to have it not show the whitespace font for whitespace only lines.

Empty lines separating indenting code are sometimes indented at the code level and sometimes not indented at all, and I don't want to draw my attention to a line I don't care to change.

I'd like to stick to built-in emacs modules, but I'm willing to use whitespace.el but it's not clear how to configure it to do this.

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
gabeiscoding
  • 524
  • 5
  • 9

2 Answers2

11

Since you want to use built-in modules, I'd advise using the whitespace.el link you specified - as it is shipped with Emacs 23. This answer works when using that whitespace.

As long as you have 'trailing in your 'whitespace-style variable (which it is by default), the following change to the regular expression for what indicates "trailing" whitespace should give you what you want:

(setq whitespace-trailing-regexp
  "\\b\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$")

Note: It is just the default value, with the \b prepended, indicating that the whitespace should follow a word.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
2

With

"\\b.*\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$"

the word does not need to be directly in front of the trailing whitespaces but there can be e.g. punctuation characters between them (i.e. this also highlights trailing whitespaces behind non-word characters).

Edit:
Using

"\\b.*?\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$")

highlights all the trailing whitespaces and thus eliminates the drawback mentioned in comment #1.

Marvin
  • 482
  • 5
  • 19
  • On the downside, `M-x whitespace-cleanup` doesn't remove all the white spaces any more, but only the last in the line (the highlighted one)... – Marvin Mar 08 '11 at 13:26
  • Use `M-x picture-mode` followed by `C-c C-c` to remove all trailing whitespace. – Drew Sep 04 '11 at 23:20
  • I cannot get this proposed solution to work in Emacs 26.3. I'm using "\\b.*?\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$" as the value of whitespace-trailing-regexp, and lines consisting of all white space are sadly still highlighted in red. – Jim Newton Apr 07 '20 at 08:49
  • @jim-newton Your expression includes `\(\(` instead of `\\(\\(`. Same with `)`. Or is it a pasting issue? – Marvin Feb 03 '21 at 18:06