Skip to content

Instantly share code, notes, and snippets.

@j05h
Created August 30, 2013 13:55
Show Gist options
  • Save j05h/6390103 to your computer and use it in GitHub Desktop.
Save j05h/6390103 to your computer and use it in GitHub Desktop.
Strip trailing whitespace on VIM save
"""""""""""""""" Functions """"""""""""""""
function! <SID>StripTrailingWhitespaces()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
%s/\s\+$//e
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
highlight OverLength ctermbg=yellow ctermfg=white guibg=#1111FF
match OverLength /\%>80v.\+$/
endfunction
"Strip whitespace on save
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment