When using Whitespace Mode with the empty style enabled, the blank lines at the end of a file aren't highlighted if I'm on the very last line. However, I also don't want them to be highlighted if I'm on the line before last, since it visually interferes with appending text to the end of a file. For instance, say I have an unsaved buffer which looks like this, where $ denotes a newline character and | denotes point:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.|$

Now if I decide to add a new paragraph:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.$

Whitespace Mode will now highlight the two blank lines at the end. However, as soon as I start writing text, it becomes obvious that there isn't, in fact, any issue with those blank lines since my point is positioned to rectify them:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.$
Pellentesque quis metus eget urna dictum pulvinar.|$

Therefore, how can I prevent Whitespace Mode from highlighting the blank lines in the situation just before I start typing Pellentesque, when my point is on the second-to-last blank line?

  • What's yours whitespace-style set to? Sep 24 '14 at 19:54
  • @mgoszcz2 (face trailing empty space-before-tab::tab lines-tail)
    – Lily Chung
    Sep 24 '14 at 19:56
  • I can't seem to be able to replicate this. For me empty lines don't show until I move the cursor away. Sep 24 '14 at 20:09
  • @mgoszcz2 Are you certain that the situation with the newline characters and point is exactly as described in the question? If point is after the very last newline character then the empty lines don't show for me. But I want them not to show even if point is just before the very last newline character.
    – Lily Chung
    Sep 24 '14 at 20:12
  • I'm not sure by now. Here's a video of how it works for me, youtube.com/watch?v=umEvhK3wVhs Sep 24 '14 at 20:37

I don't think whitespace-mode can do what you're asking out-of-the-box.

But this is Emacs! You can get (maybe?) what you want by redefining the functions whitespace-empty-at-eob-regexp and whitespace-post-command-hook.

In whitespace-empty-at-eob-regexp (the function, not the actual regexp) you should find the following branch:

 ;; at eob
 ((= limit e)
  (when (/= whitespace-point e)
    (goto-char limit)
    (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))

If you change the last line I quoted to look like this

    (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp whitespace-point)))

Then whitespace-mode will only highlight lines from point to the end. This probably won't do what you expect, though, because whitespace-mode is clever about when it refontifies. So!

In whitespace-post-command-hook you should find the following logic:

(let ((refontify
        ;; it is at end of buffer (eob)
        (= whitespace-point (1+ (buffer-size)))
        ;; the buffer was modified and ...

If you insert at the same depth a clause like this

        ;; it is inside eob whitespace region
        (>= whitespace-point whitespace-eob-marker)

Then you'll get refontification more often. I wish I could say "whenever point is in the empty space at the end of the buffer" but I'm pretty confident there are situations where you can trick Emacs into getting out of sync.

You could also do what I do, and just type really fast in the blank lines.

  • Thanks for the answer! I'll test this out when I have some time.
    – Lily Chung
    Oct 8 '14 at 23:14
  • I got this working by using (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp (+ 1 whitespace-point)))); I also couldn't get the refontification working correctly so I just set it to occur after every command. Thank you very much!
    – Lily Chung
    Jan 29 '15 at 1:54

I don't have an answer for doing what you want with whitespace-mode. Perhaps someone else will.

An alternative is to use library Highlight Chars. It doesn't have this problem when highlighting trailing whitespace. It doesn't treat newline chars as whitespace.


  • Oops! I accidentally put the wrong style in the question- I meant empty, not lines-tail. The issue is that I do want empty lines at the beginning or end to be highlighted, just not when my point is in position to fix it.
    – Lily Chung
    Sep 24 '14 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.