Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Last active August 29, 2015 14:26
Show Gist options
  • Save ELLIOTTCABLE/03df27be3264d7394819 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/03df27be3264d7394819 to your computer and use it in GitHub Desktop.
Some VimScript to *toggle* the hilighting of unwanted trailing whitespace.
" This toggles the hilighting of trailing-whitespace.
fun! ToggleExtraWhitespace()
if exists('b:ews') && b:ews == 1
"echom "Disabling trailing-whitespace hilighting in" bufnr('%') "..."
let b:ews=0
call HighlightExtraWhitespace()
"echom "-- Removing ExtraWhitespace augroup"
au! ExtraWhitespace
augroup! ExtraWhitespace
else
"echom "Enabling trailing-whitespace hilighting in" bufnr('%') "..."
let b:ews=1
call HighlightExtraWhitespace()
"echom "-- Adding ExtraWhitespace augroup"
augroup ExtraWhitespace
au!
au BufEnter * match ExtraWhitespace /\s\+$/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave * match ExtraWhiteSpace /\s\+$/
augroup END
if mode() == "i" | do ExtraWhitespace InsertEnter | else | do ExtraWhitespace BufEnter | endif
endif
endfun
" This adds (or removes) the actual hilighting to your ColourScheme. (It's must be re-called every
" time you toggle hilighting, or change your scheme.)
fun! HighlightExtraWhitespace()
if exists('b:ews') && b:ews == 1
"echom "-- Adding ExtraWhitespace hilighting"
highlight ExtraWhitespace ctermbg=red guibg=red
else
"echom "-- Removing ExtraWhitespace hilighting"
highlight clear ExtraWhitespace
endif
endfun
au ColorScheme * call HighlightExtraWhitespace()
" (Uncomment the following line if you want trailing-whitespace hilighted by default!)
"bufdo call ToggleExtraWhitespace() | au BufAdd * call ToggleExtraWhitespace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment