diff --git a/README.md b/README.md index 659172c..53d6cd6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. * If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`: ``` - autocmd BufWritePre * StripWhitespace + autocmd BufEnter * EnableStripWhitespaceOnSave ``` For exceptions of all see ```g:better_whitespace_filetypes_blacklist```. @@ -85,7 +85,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. the following to your `~/.vimrc`: ``` - autocmd FileType autocmd BufWritePre StripWhitespace + autocmd FileType autocmd BufEnter EnableStripWhitespaceOnSave ``` where `` is a comma separated list of the file types you want diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index ece91cc..9c526f3 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -156,16 +156,27 @@ function! s:StripWhitespace( line1, line2 ) call cursor(l, c) endfunction +" Strip whitespace on file save +function! s:EnableStripWhitespaceOnSave() + let g:strip_whitespace_on_save = 1 + call Echo("Strip Whitespace On Save: Enabled") + call SetupAutoCommands() +endfunction + +" Don't strip whitespace on file save +function! s:DisableStripWhitespaceOnSave() + let g:strip_whitespace_on_save = 0 + call Echo("Strip Whitespace On Save: Disabled") + call SetupAutoCommands() +endfunction + " Strips whitespace on file save function! s:ToggleStripWhitespaceOnSave() - if g:strip_whitespace_on_save == 0 - let g:strip_whitespace_on_save = 1 - call Echo("Strip Whitespace On Save: Enabled") + if g:strip_whitespace_on_save == 1 + call DisableStripWhitespaceOnSave() else - let g:strip_whitespace_on_save = 0 - call Echo("Strip Whitespace On Save: Disabled") + call EnableStripWhitespaceOnSave() endif - call SetupAutoCommands() endfunction " Determines if whitespace highlighting should currently be skipped @@ -175,6 +186,10 @@ endfunction " Run :StripWhitespace to remove end of line whitespace command! -range=% StripWhitespace call StripWhitespace( , ) +" Run :EnableStripWhitespaceOnSave to enable whitespace stripping on save +command! EnableStripWhitespaceOnSave call EnableStripWhitespaceOnSave() +" Run :DisableStripWhitespaceOnSave to disable whitespace stripping on save +command! DisableStripWhitespaceOnSave call DisableStripWhitespaceOnSave() " Run :ToggleStripWhitespaceOnSave to enable/disable whitespace stripping on save command! ToggleStripWhitespaceOnSave call ToggleStripWhitespaceOnSave() " Run :EnableWhitespace to enable whitespace highlighting