Is there a way to highlight (set the background color for example) the space before a line, when it's indentation isn't aligned to the current indentation-width?

4 spaces indentation for eg:

fn my_func() {
    if foo() {

where the # characters represent the background showing a different color, back until the indentation is aligned to 4.

Of course this is only useful when code follows strict indentation conventions.

Note that I'm aware of emacs advanced auto-indentation features and indent-highlighting plugins, but would prefer something less intrusive that only shows up when there is an issue. A little like highlighting trailing spaces.

  • This doesn't answer your question, but functions like c-indent-defun will correct indentation for you, which might achieve the desired outcome.
    – Tyler
    Feb 14, 2017 at 18:18
  • @Tyler, the issue is you're not always the only person writing the code. Checking out files written by others is quite a common use case - and you don't always want to re-indent their work either (bug-fixing or changes for code-review), so just seeing odd-indentation is useful.
    – ideasman42
    Feb 14, 2017 at 23:37
  • That makes sense. Just checking - sometimes people ask how to get half-way to something, not realizing that Emacs will take you all the way to the goal. But your use-case is reasonable.
    – Tyler
    Feb 14, 2017 at 23:41

2 Answers 2


Using font-lock...

(font-lock-add-keywords nil
  '(("^ \\{4\\}*\\( \\{1,3\\}\\)[^ ]" 1 'match)))
  • Maybe nice to use whitespace-trailing instead of match
    – ideasman42
    Feb 15, 2017 at 2:30
  • 1
    Modified to read the current tab width (format "^ \\{%d\\}*\\( \\{1,%d\\}\\)[^ ]" tab-width (- tab-width 1)) `
    – ideasman42
    Feb 15, 2017 at 12:40
  • added a slightly expanded answer, feel free to copy from it.
    – ideasman42
    Feb 15, 2017 at 12:49

This can be done using font-lock-add-keywords.

The example below shows you can can selectively enable this feature in your configuration - for different formats.

  • Note the indentation is read from tab-width.
  • This example uses whitespace-trailing, you may want to use match or any other color.


;; highlight non-aligning indent offset
(defun highlight-indent-offset ()
  `((,(lambda (limit)
         (format "^ \\{%d\\}*\\( \\{1,%d\\}\\)[^ ]" tab-width (- tab-width 1))
         limit t))
     1 'whitespace-trailing))))

Then this function can be called for spesific modes: eg,

 (lambda ()
   (setq tab-width 4)
   (setq indent-tabs-mode nil)))

This is a minor changes to @politza's answer.

  • Why not go all the way and make it a minor-mode.
    – politza
    Feb 16, 2017 at 4:10
  • I've never written a minor mode before, will need to look into it - what advantages would it have?
    – ideasman42
    Feb 16, 2017 at 4:48
  • A minor-mode can be disabled.
    – politza
    Feb 16, 2017 at 4:58

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.