Axel
Axel

Reputation: 811

Highlight unwanted spaces in Vim, except for those in help buffers

I am using these lines in my .vimrc file, to highlight leading whitespace (which includes spaces):

highlight ExtraWhitespace ctermbg=darkgreen guibg=darkcyan
autocmd BufWinEnter * match ExtraWhitespace /^\s* \s*\|\s\+$

So, this works fine in most cases. Except in help buffers, where it highlights a lot of indentation from the help files. I find this annoying, so I tried this as a workaround:

autocmd FileType help highlight clear ExtraWhitespace

But unfortunately, as soon as a help buffer is opened, it seems that the highlighting in all my buffers disappears. Any ideas on how to improve this?

Upvotes: 0

Views: 991

Answers (3)

ib.
ib.

Reputation: 28944

Change the autocommand disabling custom highlighting in help buffers as follows:

:autocmd FileType help match none

Upvotes: 0

dorserg
dorserg

Reputation: 5704

This seems to be working:

highlight ExtraWhitespace ctermbg=darkgreen guibg=darkcyan
autocmd BufEnter * if &ft != 'help' | match ExtraWhitespace /\s\+$/ | endif
autocmd BufEnter * if &ft == 'help' | match none /\s\+$/ | endif

[edit] The above code works for trailing spaces since my Vim couldn't understand your pattern. So just :%s/ubstitute the pattern to fit your needs.

Upvotes: 2

Trenton Schulz
Trenton Schulz

Reputation: 778

If you only need the whitespace errors for C/C++ files you can always use:

let c_space_errors=1

There are other languages that have similar support.

Worst case, if you need it for other types of files, you could always switch your file type to C and fix your errors then.

Upvotes: 1

Related Questions