Skip to content

Instantly share code, notes, and snippets.

@rotated8
Created February 23, 2016 19:35
Show Gist options
  • Save rotated8/c04cd77f3109777acaa5 to your computer and use it in GitHub Desktop.
Save rotated8/c04cd77f3109777acaa5 to your computer and use it in GitHub Desktop.
Highlight mixed and trailing whitespace
" Bad whitespace shows up in red.
highlight BadWhitespace ctermbg=1 guibg=Red
" This function will clear bad whitespace highlighting in help files.
" It is needed because Filetype events fire after BufEnter ones. See below.
function! IgnoreHelpFiles()
if &filetype ==? 'help'
call clearmatches()
endif
endfunction
" Match whenever we enter a buffer. (These happen in order)
augroup badwhitespace
autocmd!
" Tabs at the beginning of a line are bad. Especially if they are mixed in with spaces.
autocmd BufEnter * call matchadd('BadWhitespace', '^\( *\t\+\)\+')
" Trailing whitespace is bad, too.
autocmd BufEnter * call matchadd('BadWhitespace', '\s\+$')
" Remove matches in help files.
autocmd BufEnter * call IgnoreHelpFiles()
augroup END
@rotated8
Copy link
Author

Add this to your .vimrc to highlight spaces and tabs at the end of lines, and tabs at the beginning of lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment