Matthew Stopa
Matthew Stopa

Reputation: 3865

How would I have vim highlight redundant white space and all tabs?

I found the following code that will highlight all unnecessary whitespace, but I really want it to also highlight all the tabs in my code. I played around with a lot of variations that didn't work but I couldn't find a regex that would do both. Any ideas?

highlight RedundantWhitespace ctermbg=red guibg=red
match RedundantWhitespace /\s\+$\| \+\ze\t/

Edit: adding samples by request:

Okay so in the samples below I am using \t to represent tab and % to represent a trailing whitespace that I want vim to highlight in red.

/tOh hi here is some text%%%%
/t/tHere is some indented text%%%

So on the first line there are 1 tab that should have their spaces highlighted in red and 4 trailing spaces to have highlighted in red. On the second line there are 2 tabs and 3 trailing whitespaces to have highlighted in red.

Upvotes: 8

Views: 3830

Answers (3)

overthink
overthink

Reputation: 24443

I'd recommend using listchars rather than syntax highlighting. This would work across the board for all file types. You can use listchars for trailing spaces too, and mess with the colours as well:

set listchars=tab:»·,trail:·
set list
hi SpecialKey ctermbg=red ctermfg=red guibg=red guifg=red

Note that the background and foreground colours are the same here, so you end up seeing red "blocks" for trailing space and tabs.

Upvotes: 8

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

From your comment on another answer:

No I am looking for it to highlight every tab and all trailing spaces. I am really looking to identify any and all tabs

Does this do what you want?

match RedundantWhitespace /\s\+$\|\t/

In human terms, this is:

Match any spaces at the end of a line, or any tabs anywhere

It seems to select the white space in your examples.

Upvotes: 4

Herbert Sitz
Herbert Sitz

Reputation: 22226

I think you want to use \zs (for "start") rather than \ze (for "end"):

highlight RedundantWhitespace ctermbg=red guibg=red
match RedundantWhitespace /\s\+$\| \+\zs\t/

That will still only highlight tabs that are preceded by one or more spaces, though. Not sure if that's what you want or not. Below is a version that will highlight all tabs:

highlight RedundantWhitespace ctermbg=red guibg=red
match RedundantWhitespace /\s\+$\|\t/

Upvotes: 1

Related Questions