Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 704
Matthew Stopa Avatar asked Sep 08 '11 02:09

Matthew Stopa


People also ask

How do I show tabs and spaces in Vim?

A quick way to visualize whether there is a Tab character is by searching for it using Vim's search-commands : In NORMAL mode, type /\t and hit <Enter> . It will search for the Tab character ( \t ) and highlight the results.

How do I show whitespace in vi?

To see all the spaces inserted within this file of data, you need to add the “hls” command on the command area of Vim. So, after adding the text within the Vim text file, navigate towards the normal mode by pressing the Esc key. Press the “:” character to open the command area.

How do I delete unwanted spaces in Vim?

One way to make sure to remove all trailing whitespace in a file is to set an autocmd in your . vimrc file. Every time the user issues a :w command, Vim will automatically remove all trailing whitespace before saving.


2 Answers

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.

like image 132
overthink Avatar answered Nov 09 '22 13:11

overthink


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.

like image 36
Merlyn Morgan-Graham Avatar answered Nov 09 '22 13:11

Merlyn Morgan-Graham