From 28c4d921b52ad2eceb7967c2a6f98b3c24114be0 Mon Sep 17 00:00:00 2001 From: "J. Lewis Muir" Date: Mon, 18 Dec 2017 12:53:55 -0600 Subject: [PATCH 01/12] Fix typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4bc6951..e4cc3d3 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ A: No, and no. Sorry, but both are outside the scope of this plugin. The purp A: If you know of a better way to do something I am attempting in this plugin, or if I am doing something improperly/not reccomended then let me know! Please either open an issue informing me or make the changes yourself and open a pull request. If I am doing something that is bad - or can be improved, I more than willing to hear about it! + or can be improved, I am more than willing to hear about it! ## Promotion If you like this plugin, please star it on Github and vote it up at Vim.org! From c0ad20c3248e27fc1040dc7f98f41256b1e882c0 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Tue, 23 Jan 2018 19:42:56 +0100 Subject: [PATCH 02/12] Buffer-local stripping of whitespace --- README.md | 25 ++++++++++++++++++------- plugin/better-whitespace.vim | 24 +++++++++++++++--------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 4bc6951..c31a976 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,10 @@ Whitespace highlighting is enabled by default, with a highlight color of red. highlight ExtraWhitespace ctermbg= ``` -* To toggle whitespace highlighting on/off, call: +* To enable/disable/toggle whitespace highlighting in a buffer, call one of: ``` + :EnableWhitespace + :DisableWhitespace :ToggleWhitespace ``` @@ -51,7 +53,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. * The level `soft` will use syntax based highlighting, so there shouldn't be a performance hit like with the `hard` option. The drawback is that this - highlighting will have a lower priority and may be overwritten by higher + highlighting will have a lower priority and may be overwritten by higher priority highlighting. * To re-enable highlighting for the current line in normal mode: @@ -67,8 +69,10 @@ Whitespace highlighting is enabled by default, with a highlight color of red. the file that it cleans, either give it a range or select a group of lines in visual mode and then execute it. -* To enable/disable stripping of extra whitespace on file save, call: +* To enable/disable stripping of extra whitespace on file save for a buffer, call one of: ``` + :EnableStripWhitespaceOnSave + :DisableStripWhitespaceOnSave :ToggleStripWhitespaceOnSave ``` This will strip all trailing whitespace everytime you save the file for all file types. @@ -76,7 +80,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 BufEnter * EnableStripWhitespaceOnSave + let g:strip_whitespace_on_save = 1 ``` For exceptions of all see ```g:better_whitespace_filetypes_blacklist```. @@ -85,13 +89,11 @@ Whitespace highlighting is enabled by default, with a highlight color of red. the following to your `~/.vimrc`: ``` - autocmd FileType autocmd BufEnter EnableStripWhitespaceOnSave + autocmd FileType EnableStripWhitespaceOnSave ``` where `` is a comma separated list of the file types you want to be stripped of whitespace on file save ( ie. `javascript,c,cpp,java,html,ruby` ) - Note that `` is a keyword here denoting operation on the current buffer and - should stay just as it appears in the line above. * To disable this plugin for specific file types, add the following to your `~/.vimrc`: ``` @@ -109,6 +111,15 @@ Whitespace highlighting is enabled by default, with a highlight color of red. 'diff', 'gitcommit', 'unite', 'qf', 'help'] ``` + This blacklist can be overriden on a per-buffer basis using the buffer toggle enable and + disable commands presented above. For example: + ```vim + " highlight whitespace in markdown files, though stripping remains disabled by the blacklist + :autocmd FileType markdown EnableWhitespace + " Do not modify kernel files, even though their type is not blacklisted and highlighting is enabled + :autocmd BufRead /usr/src/linux* DisableStripWhitespaceOnSave + ``` + * To enable verbose output for each command, set verbosity in your `.vimrc`: ``` let g:better_whitespace_verbosity=1 diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..16a32c6 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -163,21 +163,22 @@ endfunction " Strip whitespace on file save function! s:EnableStripWhitespaceOnSave() - let g:strip_whitespace_on_save = 1 + let b: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 + let b: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 == 1 + call ShouldSkipHighlight() + if b:strip_whitespace_on_save == 1 call DisableStripWhitespaceOnSave() else call EnableStripWhitespaceOnSave() @@ -186,7 +187,12 @@ endfunction " Determines if whitespace highlighting should currently be skipped function! s:ShouldSkipHighlight() - return &buftype == 'nofile' || index(g:better_whitespace_filetypes_blacklist, &ft) >= 0 + if !exists('b:better_whitespace_enabled') + let b:better_whitespace_enabled = &buftype != 'nofile' && index(g:better_whitespace_filetypes_blacklist, &ft) == -1 + endif + if !exists('b:strip_whitespace_on_save') + let b:strip_whitespace_on_save = b:better_whitespace_enabled && g:strip_whitespace_on_save + endif endfunction " Run :StripWhitespace to remove end of line whitespace @@ -210,12 +216,12 @@ command! -nargs=* CurrentLineWhitespaceOff call CurrentLineWhitespaceOff( < command! CurrentLineWhitespaceOn call CurrentLineWhitespaceOn() " Process auto commands upon load, update local enabled on filetype change -autocmd FileType * let b:better_whitespace_enabled = !ShouldSkipHighlight() | call SetupAutoCommands() +autocmd FileType * call ShouldSkipHighlight() | call SetupAutoCommands() autocmd WinEnter,BufWinEnter * call SetupAutoCommands() autocmd ColorScheme * call WhitespaceInit() function! s:PerformMatchHighlight(pattern) - call s:InitVariable('b:better_whitespace_enabled', !ShouldSkipHighlight()) + call ShouldSkipHighlight() if b:better_whitespace_enabled == 1 exe 'match ExtraWhitespace "' . a:pattern . '"' else @@ -225,7 +231,7 @@ endfunction function! s:PerformSyntaxHighlight(pattern) syn clear ExtraWhitespace - call s:InitVariable('b:better_whitespace_enabled', !ShouldSkipHighlight()) + call ShouldSkipHighlight() if b:better_whitespace_enabled == 1 exe 'syn match ExtraWhitespace excludenl "' . a:pattern . '"' endif @@ -285,8 +291,8 @@ function! SetupAutoCommands() endif endif - " Strip whitespace on save if enabled - if g:strip_whitespace_on_save == 1 + " Strip whitespace on save if enabled. + if (exists('b:strip_whitespace_on_save') ? b:strip_whitespace_on_save : g:strip_whitespace_on_save) == 1 autocmd BufWritePre * call StripWhitespace( 0, line("$") ) endif From 71cbf1ae9c877d3fca690cc92adda674d9f5379a Mon Sep 17 00:00:00 2001 From: Cimbali Date: Tue, 23 Jan 2018 21:00:05 +0100 Subject: [PATCH 03/12] Add option to skip whitespace only lines, closes #65 --- README.md | 5 +++++ plugin/better-whitespace.vim | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4bc6951..df538a6 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,11 @@ Whitespace highlighting is enabled by default, with a highlight color of red. let g:better_whitespace_verbosity=1 ``` +* To skip lines that contain only whitespace, set the following in your `.vimrc`: + ``` + let g:better_whitespace_skip_empty_lines=1 + ``` + ## Supported Whitespace Characters Due to the fact that the built-in whitespace character class for patterns (`\s`) only matches against tabs and spaces, this plugin defines its own list of diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..e668929 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -41,8 +41,13 @@ call s:InitVariable('g:better_whitespace_verbosity', 0) " Define custom whitespace character group to include all horizontal unicode " whitespace characters. Vim's '\s' class only includes ASCII spaces and tabs. -let s:whitespace_group='[\u0009\u0020\u00a0\u1680\u180e\u2000-\u200b\u202f\u205f\u3000\ufeff]' -let s:eol_whitespace_pattern = s:whitespace_group . '\+$' +let s:whitespace_chars='\u0009\u0020\u00a0\u1680\u180e\u2000-\u200b\u202f\u205f\u3000\ufeff' +let s:eol_whitespace_pattern = '[' . s:whitespace_chars . ']\+$' + +call s:InitVariable('g:better_whitespace_skip_empty_lines', 0) +if g:better_whitespace_skip_empty_lines == 1 + let s:eol_whitespace_pattern = '[^' . s:whitespace_chars . ']\@1<=' . s:eol_whitespace_pattern +endif " Only init once let s:better_whitespace_initialized = 0 @@ -292,4 +297,3 @@ function! SetupAutoCommands() augroup END endfunction - From 3e56ba571829ab2c0a041a2f96106a62162e908e Mon Sep 17 00:00:00 2001 From: Cimbali Date: Tue, 23 Jan 2018 22:46:09 +0100 Subject: [PATCH 04/12] Add functions that jump to trailing white space Closes #32 --- README.md | 12 ++++++++++ plugin/better-whitespace.vim | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/README.md b/README.md index 4bc6951..3cc6f1f 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,18 @@ Whitespace highlighting is enabled by default, with a highlight color of red. let g:better_whitespace_verbosity=1 ``` +* To navigate to the previous or next trailing whitespace, you can use commands that you + can map thusly in your `.vimrc`: + ```vim + nnoremap ]w :NextTrailingWhitespace + nnoremap [w :PrevTrailingWhitespace + ``` + Note: those command take an optional range as argument, so you can for example select some + text in visual mode and search only inside it: + ```vim + :'<,'>NextTrailingWhitespace + ``` + ## Supported Whitespace Characters Due to the fact that the built-in whitespace character class for patterns (`\s`) only matches against tabs and spaces, this plugin defines its own list of diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..11e042e 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -161,6 +161,47 @@ function! s:StripWhitespace( line1, line2 ) call cursor(l, c) endfunction +" Search for trailing whitespace +function! s:GotoTrailingWhitespace(search_backwards, from, to) + " Save the current search + let _s=@/ + let l = line('.') + let c = col('.') + + " Move to start of range (if we are outside of it) + if l < a:from || l > a:to + if a:search_backwards != 0 + call cursor(a:to, 0) + call cursor(0, col('$')) + else + call cursor(a:from, 1) + endif + endif + + " Set options (search direction, last searched line) + let opts = 'wz' + let until = a:to + if a:search_backwards != 0 + let opts .= 'b' + let until = a:from + endif + " Full file, allow wrapping + if a:from == 1 && a:to == line('$') + let until = 0 + endif + + " Go to pattern + let found = search(s:eol_whitespace_pattern, opts, until) + + " Restore position if there is no match (in case we moved it) + if found == 0 + call cursor(l, c) + endif + + " Restore the saved search + let @/=_s +endfunction + " Strip whitespace on file save function! s:EnableStripWhitespaceOnSave() let g:strip_whitespace_on_save = 1 @@ -208,6 +249,9 @@ command! ToggleWhitespace call ToggleWhitespace() command! -nargs=* CurrentLineWhitespaceOff call CurrentLineWhitespaceOff( ) " Run :CurrentLineWhitespaceOn to turn on whitespace for the current line command! CurrentLineWhitespaceOn call CurrentLineWhitespaceOn() +" Search for trailing white space forwards or backwards +command! -range=% NextTrailingWhitespace call GotoTrailingWhitespace(0, , ) +command! -range=% PrevTrailingWhitespace call GotoTrailingWhitespace(1, , ) " Process auto commands upon load, update local enabled on filetype change autocmd FileType * let b:better_whitespace_enabled = !ShouldSkipHighlight() | call SetupAutoCommands() From 5a73b3cde0fbc6cd66158a37134eb749ab8e6017 Mon Sep 17 00:00:00 2001 From: Ethan Alan Hereth Date: Thu, 14 Sep 2017 13:37:49 -0400 Subject: [PATCH 05/12] Fix typo: Hightlight->Highlight --- plugin/better-whitespace.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..72b19ad 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -123,12 +123,12 @@ function! s:CurrentLineWhitespaceOff( level ) let g:current_line_whitespace_disabled_hard = 1 let g:current_line_whitespace_disabled_soft = 0 call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace "' . s:eol_whitespace_pattern . '"') - call Echo("Current Line Hightlight Off (hard)") + call Echo("Current Line Highlight Off (hard)") elseif a:level == 'soft' let g:current_line_whitespace_disabled_soft = 1 let g:current_line_whitespace_disabled_hard = 0 call s:InAllWindows("match ExtraWhitespace ''") - call Echo("Current Line Hightlight Off (soft)") + call Echo("Current Line Highlight Off (soft)") endif " Re-run auto commands with the new settings call SetupAutoCommands() @@ -142,7 +142,7 @@ function! s:CurrentLineWhitespaceOn() let g:current_line_whitespace_disabled_soft = 0 call SetupAutoCommands() call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace "' . s:eol_whitespace_pattern . '"') - call Echo("Current Line Hightlight On") + call Echo("Current Line Highlight On") endif endfunction From de40932e199cf38de10155c2612e9dd47279b584 Mon Sep 17 00:00:00 2001 From: Ethan Alan Hereth Date: Thu, 14 Sep 2017 15:04:10 -0400 Subject: [PATCH 06/12] A few more typo corrections. --- doc/better-whitespace.txt | 4 ++-- plugin/better-whitespace.vim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/better-whitespace.txt b/doc/better-whitespace.txt index 7afed4b..c93723e 100644 --- a/doc/better-whitespace.txt +++ b/doc/better-whitespace.txt @@ -38,7 +38,7 @@ This enables/disables stripping of extra whitespace on file save. *CurrentLineWhitespaceOff* Calling :CurrentLineWhitespaceOff with the option either 'hard' or 'soft' will -disable whitespace highlighting for the currentline. +disable whitespace highlighting for the current line. If 'hard' option is used, then all highlighting remains the same except that the current line is not highlighted. @@ -54,7 +54,7 @@ this option. *CurrentLineWhitespaceOn Call :CurrentLineWhitespaceOn to enable whitespace highlighting for the current -line. Highlighting is still disabled for the current line while in inserte mode. +line. Highlighting is still disabled for the current line while in insert mode. Repository exists at: http://github.com/ntpeters/vim-better-whitespace diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 72b19ad..010bf72 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -264,7 +264,7 @@ function! SetupAutoCommands() if g:current_line_whitespace_disabled_soft == 0 " Highlight all whitespace upon entering buffer call PerformMatchHighlight(s:eol_whitespace_pattern) - " Check if current line highglighting is disabled + " Check if current line highlighting is disabled if g:current_line_whitespace_disabled_hard == 1 " Never highlight whitespace on current line autocmd InsertEnter,CursorMoved,CursorMovedI * call HighlightEOLWhitespaceExceptCurrentLine('match') From 96901270ee13c3d4b2fba16c6ccc82beb9f81162 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 24 Jan 2018 15:27:29 +0100 Subject: [PATCH 07/12] More typos --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e4cc3d3..06fc21d 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. ## Supported Whitespace Characters Due to the fact that the built-in whitespace character class for patterns (`\s`) only matches against tabs and spaces, this plugin defines its own list of -horizontal whitepsace characters to match for both highlighting and stripping. +horizontal whitespace characters to match for both highlighting and stripping. This is list should match against all ASCII and Unicode horizontal whitespace characters: From 0637398e661ef0ea367daf126448df901661513e Mon Sep 17 00:00:00 2001 From: Cimbali Date: Tue, 23 Jan 2018 21:26:14 +0100 Subject: [PATCH 08/12] Add option to strip white lines at EOF, closes #67 --- README.md | 5 +++++ plugin/better-whitespace.vim | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 4bc6951..1255dac 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,11 @@ Whitespace highlighting is enabled by default, with a highlight color of red. 'diff', 'gitcommit', 'unite', 'qf', 'help'] ``` +* To strip white lines at the end of the file when stripping whitespace, set this option in your `.vimrc`: + ``` + let g:strip_whitelines_at_eof=1 + ``` + * To enable verbose output for each command, set verbosity in your `.vimrc`: ``` let g:better_whitespace_verbosity=1 diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..8c1fc59 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -32,6 +32,10 @@ call s:InitVariable('g:current_line_whitespace_disabled_soft', 0) " Set this to enable stripping whitespace on file save call s:InitVariable('g:strip_whitespace_on_save', 0) +" Set this to enable stripping white lines at the end of the file when we +" strip whitespace +call s:InitVariable('g:strip_whitelines_at_eof', 0) + " Set this to blacklist specific filetypes let default_blacklist=['diff', 'gitcommit', 'unite', 'qf', 'help', 'markdown'] call s:InitVariable('g:better_whitespace_filetypes_blacklist', default_blacklist) @@ -156,6 +160,18 @@ function! s:StripWhitespace( line1, line2 ) " Strip the whitespace silent! execute ':' . a:line1 . ',' . a:line2 . 's/' . s:eol_whitespace_pattern . '//e' + " Strip empty lines at EOF + if g:strip_whitelines_at_eof == 1 + if &ff == 'dos' + let nl='\r\n' + elseif &ff == 'max' + let nl='\r' + else " unix + let nl='\n' + endif + silent! execute '%s/\('.nl.'\)\+\%$//' + endif + " Restore the saved search and cursor position let @/=_s call cursor(l, c) From 0c3d2e5e043822cf38255c7cf48dcd6005be5355 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 24 Jan 2018 00:21:08 +0100 Subject: [PATCH 09/12] Add operator for StripWhitespace (defaults to space) Closes #18 --- README.md | 11 +++++++++++ plugin/better-whitespace.vim | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 4bc6951..b84fefc 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,17 @@ Whitespace highlighting is enabled by default, with a highlight color of red. the file that it cleans, either give it a range or select a group of lines in visual mode and then execute it. + * There is an operator (defaulting to ``) to clean whitespace. + For example, in normal mode, `ip` will remove trailing whitespace from the + current paragraph. + + You can change the operator it, for example to set it to _s, using: + ```vim + let g:better_whitespace_operator='_s' + ``` + Now `_s` strips whitespace on \ lines, and `_s` on the + lines affected by the motion given. Set to the empty string to deactivate the operator. + * To enable/disable stripping of extra whitespace on file save, call: ``` :ToggleStripWhitespaceOnSave diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 27e0c17..719c6d2 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -15,6 +15,9 @@ function! s:InitVariable(var, value) endif endfunction +" Operator for StripWhitespace (empty to disable) +call s:InitVariable('g:better_whitespace_operator', '') + " Set this to enable/disable whitespace highlighting call s:InitVariable('g:better_whitespace_enabled', 1) @@ -209,6 +212,20 @@ command! -nargs=* CurrentLineWhitespaceOff call CurrentLineWhitespaceOff( < " Run :CurrentLineWhitespaceOn to turn on whitespace for the current line command! CurrentLineWhitespaceOn call CurrentLineWhitespaceOn() +if !empty('g:better_whitespace_operator') + function! s:StripWhitespaceMotion(type) + call StripWhitespace(line("'["), line("']")) + endfunction + + " Visual mode + exe "xmap ".g:better_whitespace_operator." :StripWhitespace" + " Normal mode (+ space, with line count) + exe "nmap ".g:better_whitespace_operator." :exe '.,+'.v:count' StripWhitespace'" + " Other motions + exe "nmap ".g:better_whitespace_operator." :set opfunc=StripWhitespaceMotiong@" +endif + + " Process auto commands upon load, update local enabled on filetype change autocmd FileType * let b:better_whitespace_enabled = !ShouldSkipHighlight() | call SetupAutoCommands() autocmd WinEnter,BufWinEnter * call SetupAutoCommands() From 749af79352d480377a0742cb3b0ad7fed60a59ec Mon Sep 17 00:00:00 2001 From: matt1003 Date: Wed, 18 Jan 2017 16:02:48 +1300 Subject: [PATCH 10/12] Match spaces that appear before or in-between tabs This patch adds a option to enable matching of space characters that appear before or in-between tabs. Removing such characters can be as important as removing trailing white-space. This option is currently disabled by default. --- README.md | 5 +++++ plugin/better-whitespace.vim | 15 +++++++++++---- whitespace_examples.txt | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 95c5c02..df1b525 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,11 @@ Whitespace highlighting is enabled by default, with a highlight color of red. let g:strip_whitelines_at_eof=1 ``` +* To enable matching of space characters that appear before or in-between tabs, add the following to your `.vimrc`: + ``` + let g:match_spaces_that_precede_tabs=1 + ``` + * To enable verbose output for each command, set verbosity in your `.vimrc`: ``` let g:better_whitespace_verbosity=1 diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 63c7945..9f3f9d3 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -21,6 +21,9 @@ call s:InitVariable('g:better_whitespace_operator', '') " Set this to enable/disable whitespace highlighting call s:InitVariable('g:better_whitespace_enabled', 1) +" Set this to match space characters that appear before or in-between tabs +call s:InitVariable('g:match_spaces_that_precede_tabs', 0) + " Set this to disable highlighting on the current line in all modes " WARNING: This checks for current line on cursor move, which can significantly " impact the performance of Vim (especially on large files) @@ -47,13 +50,17 @@ call s:InitVariable('g:better_whitespace_filetypes_blacklist', default_blacklist call s:InitVariable('g:better_whitespace_verbosity', 0) " Define custom whitespace character group to include all horizontal unicode -" whitespace characters. Vim's '\s' class only includes ASCII spaces and tabs. -let s:whitespace_chars='\u0009\u0020\u00a0\u1680\u180e\u2000-\u200b\u202f\u205f\u3000\ufeff' -let s:eol_whitespace_pattern = '[' . s:whitespace_chars . ']\+$' +" whitespace characters except tab (\u0009). Vim's '\s' class only includes ASCII spaces and tabs. +let s:whitespace_chars='\u0020\u00a0\u1680\u180e\u2000-\u200b\u202f\u205f\u3000\ufeff' +let s:eol_whitespace_pattern = '[\u0009' . s:whitespace_chars . ']\+$' call s:InitVariable('g:better_whitespace_skip_empty_lines', 0) if g:better_whitespace_skip_empty_lines == 1 - let s:eol_whitespace_pattern = '[^' . s:whitespace_chars . ']\@1<=' . s:eol_whitespace_pattern + let s:eol_whitespace_pattern = '[^\u0009' . s:whitespace_chars . ']\@1<=' . s:eol_whitespace_pattern +endif + +if g:match_spaces_that_precede_tabs == 1 + let s:eol_whitespace_pattern .= '\|[' . s:whitespace_chars . ']\+\ze[\u0009]' endif " Only init once diff --git a/whitespace_examples.txt b/whitespace_examples.txt index 1f468d2..41cf9aa 100644 --- a/whitespace_examples.txt +++ b/whitespace_examples.txt @@ -19,3 +19,6 @@ U+202F NARROW NO-BREAK SPACE:  U+205F MEDIUM MATHEMATICAL SPACE:  U+3000 IDEOGRAPHIC SPACE:  U+FEFF ZERO WIDTH NO-BREAK SPACE: +xxxxxx SPACES PRECEDING TABS: hello world! +xxxxxx SPACES INBETWEEN TABS: hello world! + From 8b6dbc9e5e773afea1ed35d5cbd37d80abe6a8fb Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 24 Jan 2018 16:21:32 +0100 Subject: [PATCH 11/12] Do not strip spaces before tabs Only match them, see discussion on #58 --- README.md | 7 +++++-- plugin/better-whitespace.vim | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index df1b525..4682367 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,13 @@ Whitespace highlighting is enabled by default, with a highlight color of red. let g:strip_whitelines_at_eof=1 ``` -* To enable matching of space characters that appear before or in-between tabs, add the following to your `.vimrc`: +* To highlight space characters that appear before or in-between tabs, add the following to your `.vimrc`: ``` - let g:match_spaces_that_precede_tabs=1 + let g:show_spaces_that_precede_tabs=1 ``` + Such spaces can not be automatically removed by this plugin, though you can try `=` to fix indentation. + You can still navigate to the highlighted spaces with Next/PrevTrailingWhitespace (see below), and fix + them manually. * To enable verbose output for each command, set verbosity in your `.vimrc`: ``` diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 9f3f9d3..17515e3 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -22,7 +22,7 @@ call s:InitVariable('g:better_whitespace_operator', '') call s:InitVariable('g:better_whitespace_enabled', 1) " Set this to match space characters that appear before or in-between tabs -call s:InitVariable('g:match_spaces_that_precede_tabs', 0) +call s:InitVariable('g:show_spaces_that_precede_tabs', 0) " Set this to disable highlighting on the current line in all modes " WARNING: This checks for current line on cursor move, which can significantly @@ -59,7 +59,8 @@ if g:better_whitespace_skip_empty_lines == 1 let s:eol_whitespace_pattern = '[^\u0009' . s:whitespace_chars . ']\@1<=' . s:eol_whitespace_pattern endif -if g:match_spaces_that_precede_tabs == 1 +let s:strip_whitespace_pattern = s:eol_whitespace_pattern +if g:show_spaces_that_precede_tabs == 1 let s:eol_whitespace_pattern .= '\|[' . s:whitespace_chars . ']\+\ze[\u0009]' endif @@ -173,7 +174,7 @@ function! s:StripWhitespace( line1, line2 ) let c = col(".") " Strip the whitespace - silent! execute ':' . a:line1 . ',' . a:line2 . 's/' . s:eol_whitespace_pattern . '//e' + silent! execute ':' . a:line1 . ',' . a:line2 . 's/' . s:strip_whitespace_pattern . '//e' " Strip empty lines at EOF if g:strip_whitelines_at_eof == 1 From 11c5da7f253138535f3fb599d3e3b2742d2318f8 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 24 Jan 2018 16:59:37 +0100 Subject: [PATCH 12/12] Update README and complete examples --- README.md | 80 +++++++++++++++++++++++------------------ whitespace_examples.txt | 4 +++ 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index f78e9f3..0942e57 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Vim Better Whitespace Plugin -This plugin causes all trailing whitespace characters (see [Supported Whitespace Characters](#supported-whitespace-characters) below) to be -highlighted. Whitespace for the current line will not be highlighted -while in insert mode. It is possible to disable current line highlighting while in other -modes as well (see options below). A helper function `:StripWhitespace` is also provided -to make whitespace cleaning painless. +This plugin causes all trailing whitespace characters (see [Supported Whitespace +Characters](#supported-whitespace-characters) below) to be highlighted. Whitespace for the current line will +not be highlighted while in insert mode. It is possible to disable current line highlighting while in other +modes as well (see options below). A helper function `:StripWhitespace` is also provided to make whitespace +cleaning painless. Here is a screenshot of this plugin at work: ![Example Screenshot](http://i.imgur.com/St7yHth.png) @@ -13,36 +13,45 @@ Here is a screenshot of this plugin at work: There are a few ways you can go about installing this plugin: 1. If you have [Vundle](https://github.com/gmarik/Vundle.vim) you can simply add: - ``` + ```vim Plugin 'ntpeters/vim-better-whitespace' ``` to your `.vimrc` file then run: - ``` + ```vim :PluginInstall ``` 2. If you are using [Pathogen](https://github.com/tpope/vim-pathogen), you can just run the following command: ``` git clone git://github.com/ntpeters/vim-better-whitespace.git ~/.vim/bundle/vim-better-whitespace ``` -3. While this plugin can also be installed by copying its contents into your `~/.vim/` directory, I would highly recommend using one of the above methods as they make managing your Vim plugins painless. +3. While this plugin can also be installed by copying its contents into your `~/.vim/` directory, I would + highly recommend using one of the above methods as they make managing your Vim plugins painless. ## Usage Whitespace highlighting is enabled by default, with a highlight color of red. * To set a custom highlight color, just call: - ``` + ```vim highlight ExtraWhitespace ctermbg= ``` -* To enable/disable/toggle whitespace highlighting in a buffer, call one of: +* To enable highlighting and stripping whitespace on save by default, use respectively + ```vim + let g:better_whitespace_enabled=1 + let g:strip_whitespace_on_save=1 ``` + Set them to 0 to disable this default behaviour. See below for the blacklist of file types + and per-buffer settings. + +* To enable/disable/toggle whitespace highlighting in a buffer, call one of: + ```vim :EnableWhitespace :DisableWhitespace :ToggleWhitespace ``` * To disable highlighting for the current line in normal mode call: - ``` + ```vim :CurrentLineWhitespaceOff ``` Where `` is either `hard` or `soft`. @@ -57,12 +66,12 @@ Whitespace highlighting is enabled by default, with a highlight color of red. priority highlighting. * To re-enable highlighting for the current line in normal mode: - ``` + ```vim :CurrentLineWhitespaceOn ``` * To clean extra whitespace, call: - ``` + ```vim :StripWhitespace ``` By default this operates on the entire file. To restrict the portion of @@ -81,7 +90,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. lines affected by the motion given. Set to the empty string to deactivate the operator. * To enable/disable stripping of extra whitespace on file save for a buffer, call one of: - ``` + ```vim :EnableStripWhitespaceOnSave :DisableStripWhitespaceOnSave :ToggleStripWhitespaceOnSave @@ -90,7 +99,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`: - ``` + ```vim let g:strip_whitespace_on_save = 1 ``` @@ -99,7 +108,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. * If you would prefer to only strip whitespace for certain filetypes, add the following to your `~/.vimrc`: - ``` + ```vim autocmd FileType EnableStripWhitespaceOnSave ``` @@ -107,17 +116,17 @@ Whitespace highlighting is enabled by default, with a highlight color of red. to be stripped of whitespace on file save ( ie. `javascript,c,cpp,java,html,ruby` ) * To disable this plugin for specific file types, add the following to your `~/.vimrc`: - ``` + ```vim let g:better_whitespace_filetypes_blacklist=['', '', ''] ``` This replaces the filetypes from the default list of blacklisted filetypes. The default types that are blacklisted are: - ``` + ```vim ['diff', 'gitcommit', 'unite', 'qf', 'help', 'markdown'] ``` If you do not want any of these filetypes unignored, simply include them in the blacklist: - ``` + ```vim let g:better_whitespace_filetypes_blacklist=['', '', '', 'diff', 'gitcommit', 'unite', 'qf', 'help'] ``` @@ -132,25 +141,21 @@ Whitespace highlighting is enabled by default, with a highlight color of red. ``` * To strip white lines at the end of the file when stripping whitespace, set this option in your `.vimrc`: - ``` + ```vim let g:strip_whitelines_at_eof=1 ``` * To highlight space characters that appear before or in-between tabs, add the following to your `.vimrc`: - ``` + ```vim let g:show_spaces_that_precede_tabs=1 ``` - Such spaces can not be automatically removed by this plugin, though you can try `=` to fix indentation. + Such spaces can **not** be automatically removed by this plugin, though you can try + [`=` to fix indentation](http://vimdoc.sourceforge.net/htmldoc/change.html#=). You can still navigate to the highlighted spaces with Next/PrevTrailingWhitespace (see below), and fix them manually. -* To enable verbose output for each command, set verbosity in your `.vimrc`: - ``` - let g:better_whitespace_verbosity=1 - ``` - -* To skip lines that contain only whitespace, set the following in your `.vimrc`: - ``` +* To ignore lines that contain only whitespace, set the following in your `.vimrc`: + ```vim let g:better_whitespace_skip_empty_lines=1 ``` @@ -166,6 +171,12 @@ Whitespace highlighting is enabled by default, with a highlight color of red. :'<,'>NextTrailingWhitespace ``` +* To enable verbose output for each command, set verbosity in your `.vimrc`: + ```vim + let g:better_whitespace_verbosity=1 + ``` + + ## Supported Whitespace Characters Due to the fact that the built-in whitespace character class for patterns (`\s`) only matches against tabs and spaces, this plugin defines its own list of @@ -209,7 +220,7 @@ Here are a couple more screenshots of the plugin at work. This screenshot shows the current line not being highlighted in insert mode: ![Insert Screenthot](http://i.imgur.com/RNHR9KX.png) -This screenshot shows the current line not being highlighted in normal mode( `CurrentLineWhitespaceOff hard` ): +This screenshot shows the current line not being highlighted in normal mode(`CurrentLineWhitespaceOff hard`): ![Normal Screenshot](http://i.imgur.com/o888Z7b.png) This screenshot shows that highlighting works fine for spaces, tabs, and a mixture of both: @@ -264,10 +275,11 @@ A: It is true that a large part of this is fairly simple to make a part of an i **Q: Can you add indentation highlighting for spaces/tabs? Can you add highlighting for other types of white space?** -A: No, and no. Sorry, but both are outside the scope of this plugin. The purpose of this plugin - is to provide a better experience for showing and dealing with extra white space. There is - already an amazing plugin for showing indentation in Vim called [Indent Guides](https://github.com/nathanaelkane/vim-indent-guides). - For other types of white space highlighting, [listchars](http://vimdoc.sourceforge.net/htmldoc/options.html#'listchars') should be sufficient. +A: No, and no. Sorry, but both are outside the scope of this plugin. The purpose of this plugin + is to provide a better experience for showing and dealing with extra white space. There is already an + amazing plugin for showing indentation in Vim called [Indent + Guides](https://github.com/nathanaelkane/vim-indent-guides). For other types of white space highlighting, + [listchars](http://vimdoc.sourceforge.net/htmldoc/options.html#'listchars') should be sufficient. **Q: I have a better way to do something in this plugin. OR You're doing something stupid/wrong/bad.** diff --git a/whitespace_examples.txt b/whitespace_examples.txt index 41cf9aa..78ddaa7 100644 --- a/whitespace_examples.txt +++ b/whitespace_examples.txt @@ -21,4 +21,8 @@ U+3000 IDEOGRAPHIC SPACE:  U+FEFF ZERO WIDTH NO-BREAK SPACE: xxxxxx SPACES PRECEDING TABS: hello world! xxxxxx SPACES INBETWEEN TABS: hello world! +xxxxxx A WHITESPACE-ONLY LINE AND AN EMPTY LINE AT EOF + + +