Skip to content

Commit c9bb9b4

Browse files
authored
Merge pull request #55 from docwhat/pr/strip-toggle
Added explicit Enable/Disable for strip-on-save
2 parents 429f1a0 + d9ff9bd commit c9bb9b4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
7676
* If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`:
7777
7878
```
79-
autocmd BufWritePre * StripWhitespace
79+
autocmd BufEnter * EnableStripWhitespaceOnSave
8080
```
8181
8282
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.
8585
the following to your `~/.vimrc`:
8686
8787
```
88-
autocmd FileType <desired_filetypes> autocmd BufWritePre <buffer> StripWhitespace
88+
autocmd FileType <desired_filetypes> autocmd BufEnter <buffer> EnableStripWhitespaceOnSave
8989
```
9090
9191
where `<desired_filetypes>` is a comma separated list of the file types you want

plugin/better-whitespace.vim

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,27 @@ function! s:StripWhitespace( line1, line2 )
156156
call cursor(l, c)
157157
endfunction
158158

159+
" Strip whitespace on file save
160+
function! s:EnableStripWhitespaceOnSave()
161+
let g:strip_whitespace_on_save = 1
162+
call <SID>Echo("Strip Whitespace On Save: Enabled")
163+
call <SID>SetupAutoCommands()
164+
endfunction
165+
166+
" Don't strip whitespace on file save
167+
function! s:DisableStripWhitespaceOnSave()
168+
let g:strip_whitespace_on_save = 0
169+
call <SID>Echo("Strip Whitespace On Save: Disabled")
170+
call <SID>SetupAutoCommands()
171+
endfunction
172+
159173
" Strips whitespace on file save
160174
function! s:ToggleStripWhitespaceOnSave()
161-
if g:strip_whitespace_on_save == 0
162-
let g:strip_whitespace_on_save = 1
163-
call <SID>Echo("Strip Whitespace On Save: Enabled")
175+
if g:strip_whitespace_on_save == 1
176+
call <SID>DisableStripWhitespaceOnSave()
164177
else
165-
let g:strip_whitespace_on_save = 0
166-
call <SID>Echo("Strip Whitespace On Save: Disabled")
178+
call <SID>EnableStripWhitespaceOnSave()
167179
endif
168-
call <SID>SetupAutoCommands()
169180
endfunction
170181

171182
" Determines if whitespace highlighting should currently be skipped
@@ -175,6 +186,10 @@ endfunction
175186

176187
" Run :StripWhitespace to remove end of line whitespace
177188
command! -range=% StripWhitespace call <SID>StripWhitespace( <line1>, <line2> )
189+
" Run :EnableStripWhitespaceOnSave to enable whitespace stripping on save
190+
command! EnableStripWhitespaceOnSave call <SID>EnableStripWhitespaceOnSave()
191+
" Run :DisableStripWhitespaceOnSave to disable whitespace stripping on save
192+
command! DisableStripWhitespaceOnSave call <SID>DisableStripWhitespaceOnSave()
178193
" Run :ToggleStripWhitespaceOnSave to enable/disable whitespace stripping on save
179194
command! ToggleStripWhitespaceOnSave call <SID>ToggleStripWhitespaceOnSave()
180195
" Run :EnableWhitespace to enable whitespace highlighting

0 commit comments

Comments
 (0)