Like this: enter image description here

I have been able to get whitespace-mode to display dots for leading spaces.

enter image description here

But I want to specially display each sequence of 2 leading spaces, with maybe a vertical bar.


Maybe not with whitespace-mode?

As a separate question, how do I get whitespace-mode to display only leading spaces as middle-dot?

I have

    whitespace-space-regexp "\\(^ +\\)" 

...but that seems only to apply to the FACE used, not to the character substitution.

I've been fiddling with this for 20 minutes. I'm amazed that I still have not cracked this. Doesn't everyone want something like this?

  • The existing vertical bar solutions in Elisp are not that good, but you may wish to play around with vline.el and the libraries that build upon / modify that library. I am working on feature requests 17684 and 22873 in my spare time, but I am probably a year or two (or more) away from perfecting them. [It just depends upon how spare time I have, combined with motivation ... The next patch proof concept will probably be available in about a month or so ...] Fill-column-indicator by Alp Aker may also be of interest to you -- it an be tweaked ...
    – lawlist
    Sep 19, 2018 at 4:26
  • See also: superuser.com/a/608910/206164 and other threads that can be found with Googling things like: emacs vertical highlight; emacs verticular ruler, etc.
    – lawlist
    Sep 19, 2018 at 4:35

1 Answer 1


Relying on font-lock and compose-region, I can sort of get emacs to do what I want. This stuff:

(defun yaml-pretty-search-leading-ws (limit)
  "Function to search for leading whitespace, and invoke
`compose-region' on ranges within it, to display the whitespace
differently: display a dot and a vertical bar for the sequence.

This fn is intended to be invoked indirectly via font-lock as a
search/matcher. It doesn't return a positive match to font-lock.

At the finish, all of the leading whitespace up to LIMIT should
be composed and fontified."

  (let ((start (point)))
    (save-match-data ;; we don't want font-lock to fontify.
      ;; 2 consecutive spaces
      (while (re-search-forward "  " limit t)
        (let ((start (match-beginning 0))
              (stop (match-end 0)))
          (put-text-property start stop 'face 'yaml-pretty-leading-ws-face)
          (compose-region start (1- stop) ?·)
          (compose-region (1- stop) stop ?|)

(defvar yaml-pretty-flock-keywords
  '((yaml-pretty-search-leading-ws . yaml-pretty-leading-ws-face)))

(font-lock-add-keywords nil yaml-pretty-flock-keywords)

Results in :

enter image description here

For static text display, this is exactly what I wanted.

Now I need to get it to do the inverse - "un-compose" the region, and remove the face property - as I modify the leading whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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