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

Up vote 4 down vote favorite 1 share g+ share fb share tw.

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.

Regex vim editor vi macvim link|improve this question edited Sep 8 '11 at 3:57 asked Sep 8 '11 at 2:14John Baker634414 81% accept rate.

If you can give some samples and counter-samples that will make it easier to match said samples with a regex. Test-driven development ;) – Merlyn Morgan-Graham Sep 8 '11 at 3:50 After struggling with this I have to mention this has to be in . Gvimrc, not .

Vimrc! – philant Mar 23 at 11:26.

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.

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 – John Baker Sep 8 '11 at 4: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: vimdoc.sourceforge.net/htmldoc/usr_27.html – Merlyn Morgan-Graham Sep 8 '11 at 4: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 8 '11 at 7:37 If you are adding any more than this rule, or start adding to this rule, I agree with Prince. – Merlyn Morgan-Graham Sep 8 '11 at 7: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.

– John Baker Sep 8 '11 at 13:47.

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.

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 – John Baker Sep 8 '11 at 3: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? – John Baker Sep 8 '11 at 3: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 8 '11 at 14:16 That worked in MacVim but not regular vim in the console. Not that I know why.

– John Baker Sep 8 '11 at 19:19.

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.

No I am looking for it to highlight every tab and all trailing spaces. I am really looking to identify any and all tabs. – John Baker Sep 8 '11 at 3:19 Then just change to second version I added above.

. . – Herbert Sitz Sep 8 '11 at 3:49 Your version actually doesn't seem to highlight any tabs at all.

– John Baker Sep 8 '11 at 3:51 @John -- Hmm, you must have typed it in wrong, my second version was identical to the one you later accepted as an answer – Herbert Sitz Sep 8 '11 at 4:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions