Skip to content

Instantly share code, notes, and snippets.

@ferreum
Last active August 29, 2015 13:56
Show Gist options
  • Save ferreum/8970858 to your computer and use it in GitHub Desktop.
Save ferreum/8970858 to your computer and use it in GitHub Desktop.
vim trailingspace plugin
" File: trailingspace.vim
" Description: highlight trailing spaces
" Author: ferreum github.com/ferreum
" Licence: MIT
" Created: 2013-08-02
" Last Change: 2014-02-13
let s:save_cpo = &cpo
set cpo&vim
augroup plugin_trailingspace
autocmd!
autocmd ColorScheme *
\ call trailingspace#define_hi()
autocmd FileType,BufNew,WinEnter *
\ call trailingspace#define_match()
augroup END
function! trailingspace#define_hi() abort
hi TrailingSpace ctermfg=124 guifg=#AF0000 ctermbg=NONE guibg=NONE cterm=underline gui=underline
endfunction
function! trailingspace#define_match() abort
if exists('w:trailingspace_matchid')
let id = w:trailingspace_matchid
silent! call matchdelete(id)
endif
if trailingspace#is_disabled()
return 0
endif
if &ft ==# 'unite'
unlet! w:trailingspace_matchid
return 0
endif
let w:trailingspace_matchid = matchadd('TrailingSpace', '\v%(\s%#@1<!)+$', -200)
return w:trailingspace_matchid
endfunction
function! trailingspace#is_disabled() abort
return exists('w:trailingspace_disable')
\ ? w:trailingspace_disable
\ : (exists('g:trailingspace_disable') && g:trailingspace_disable)
endfunction
function! trailingspace#enable(...) abort
let opt = get(a:000, 0, "")
if opt =~# 'w' || empty(opt)
unlet! w:trailingspace_disable
call trailingspace#define_match()
elseif opt =~# 'g'
unlet! g:trailingspace_disable
windo call trailingspace#define_match()
endif
return
endfunction
function! trailingspace#disable(bang, ...) abort
let opt = get(a:000, 0, "")
if opt =~# 'w' || empty(opt)
let w:trailingspace_disable = empty(a:bang) || !trailingspace#is_disabled()
call trailingspace#define_match()
elseif opt =~# 'g'
let g:trailingspace_disable = empty(a:bang) || !exists('g:trailingspace_disable') || !g:trailingspace_disable
windo call trailingspace#define_match()
endif
return
endfunction
call trailingspace#define_hi()
call trailingspace#define_match()
command! -nargs=? -bang TrailingSpaceDisable call trailingspace#disable("<bang>", <f-args>)
command! -nargs=? TrailingSpaceEnable call trailingspace#enable(<f-args>)
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set sw=3 ts=3 sts=0 et sta sr ft=vim fdm=syntax:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment