8

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.

Matthew Stopa
  • 3,793
  • 6
  • 42
  • 51

3 Answers3

8

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.

overthink
  • 23,985
  • 4
  • 69
  • 69
  • Ahh that's a bummer. I really just want the characters to be highlighted in red and that's it. This adds a bunch of symbols – Matthew Stopa Sep 08 '11 at 03:05
  • Better question: Is there anyway to make this a solid color so that you can't see anything other than just red? Meaning no foreground color? – Matthew Stopa Sep 08 '11 at 03:18
  • Set the foreground and background colours the same? I updated the example above to do this. The characters are still there, but they just appear as red blocks unless you put a cursor on them. – overthink Sep 08 '11 at 14:16
  • That worked in MacVim but not regular vim in the console. Not that I know why. – Matthew Stopa Sep 08 '11 at 19:19
4

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.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I literally just figured this out and came here to answer my own question. That does it perfectly. I was used to regex using | for or so I thought \| was escaping it but it wasn't. That was actually still an or. Anyway, I upvoted everyone who submitted an answer and I appreciate everyone's help. This was what I needed though. Thanks – Matthew Stopa Sep 08 '11 at 04:06
  • @John: Yep. Like you said, `\|` is for the "or" in VIM. `|` is not - it is the literal character. Type `:help regex` and it is the first thing that pops up :) This might help, too: http://vimdoc.sourceforge.net/htmldoc/usr_27.html – Merlyn Morgan-Graham Sep 08 '11 at 04:08
  • 1
    Instead of using the `\|` character, I would find it clearer to have two separate match statements, one for trailing whitespace and one for tabs. This makes it more transparent and easier to maintain. But that's just my personal preference. – Prince Goulash Sep 08 '11 at 07:37
  • If you are adding any more than this rule, or start adding to this rule, I agree with Prince. – Merlyn Morgan-Graham Sep 08 '11 at 07:53
  • I tried this actually but it ends up that for whatever reason it will only enforce the last rule I enter in the file. I had tried that originally and it didn't work for me. – Matthew Stopa Sep 08 '11 at 13:47
  • That could happen if you were using the same name twice. You need to give each match pattern a different name, e.g., RedundantSpaces and RedundantTabs, then set both up to have same highlight in two different highlight statements. – Herbert Sitz Sep 08 '11 at 14:27
  • They were named differently but it didn't seem to matter. I'll try again some time though. – Matthew Stopa Sep 08 '11 at 19:20
1

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/
Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54