I have some code in my ~/.vimrc to highlight trailing whitespace.

" Highlight trailing whitespace
hi TrailingWhitespace ctermbg=red guibg=red
matchadd("TrailingWhitespace", '\v\s+$')

I only want to see trailing whitespace when I am in normal mode, not when I am in insert mode.

highlighting occurs when in insert mode

How can I accomplish that highlighting only occurs in normal mode?

1 Answer 1


The Vim Better Whitespace Plugin does exactly what I want:

This plugin causes all trailing whitespace characters (see Supported Whitespace Characters below) to be highlighted. Whitespace for the current line will not be highlighted while in insert mode. It is possible to disable current line highlighting while in other modes as well (see options below).

Install the plugin (or copy the content of better-whitespace.vim) and add this line to your .vimrc:

hi ExtraWhitespace ctermbg=red guibg=red

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.