Skip to content

Commit ca47857

Browse files
authored
Update operator default to <leader>s (#79)
This change updates the default operator mapping to `<leader>s`, and adds a check to ensure the mapping does not already exist. Fixes #78
1 parent 8efb0ba commit ca47857

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
7878
the file that it cleans, either give it a range or select a group of lines
7979
in visual mode and then execute it.
8080
81-
* There is an operator (defaulting to `<space>`) to clean whitespace.
82-
For example, in normal mode, `<space>ip` will remove trailing whitespace from the
81+
* There is an operator (defaulting to `<leader>s`) to clean whitespace.
82+
For example, in normal mode, `<leader>sip` will remove trailing whitespace from the
8383
current paragraph.
8484
8585
You can change the operator it, for example to set it to _s, using:
@@ -89,6 +89,9 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
8989
Now `<number>_s<space>` strips whitespace on \<number\> lines, and `_s<motion>` on the
9090
lines affected by the motion given. Set to the empty string to deactivate the operator.
9191
92+
Note: This operator will not be mapped if an existing, user-defined
93+
mapping is detected for the provided operator value.
94+
9295
* To enable/disable stripping of extra whitespace on file save for a buffer, call one of:
9396
```vim
9497
:EnableStripWhitespaceOnSave

doc/better-whitespace.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ this option.
5656
Call :CurrentLineWhitespaceOn to enable whitespace highlighting for the current
5757
line. Highlighting is still disabled for the current line while in insert mode.
5858

59+
*operator*
60+
By default, an operator is provided mapped to: <leader>s.
61+
To modify the key mapping for this operator, set g:better_whitespace_operator.
62+
This operator will strip whitespace over the currently selected region in visual
63+
mode, or for the following motion in normal mode.
64+
Note: This operator will not be mapped if an existing, user-defined mapping is
65+
detected for the provided operator value.
66+
5967
Repository exists at: http://github.com/ntpeters/vim-better-whitespace
6068

6169
Originally inspired by: https://github.com/bronson/vim-trailing-whitespace

plugin/better-whitespace.vim

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function! s:InitVariable(var, value)
1616
endfunction
1717

1818
" Operator for StripWhitespace (empty to disable)
19-
call s:InitVariable('g:better_whitespace_operator', '<space>')
19+
call s:InitVariable('g:better_whitespace_operator', '<leader>s')
2020

2121
" Set this to enable/disable whitespace highlighting
2222
call s:InitVariable('g:better_whitespace_enabled', 1)
@@ -296,14 +296,24 @@ if !empty(g:better_whitespace_operator)
296296
call <SID>StripWhitespace(line("'["), line("']"))
297297
endfunction
298298

299-
" Visual mode
300-
exe "xmap <silent> ".g:better_whitespace_operator." :StripWhitespace<CR>"
301-
" Normal mode (+ space, with line count)
302-
exe "nmap <silent> ".g:better_whitespace_operator."<space> :<C-U>exe '.,+'.v:count' StripWhitespace'<CR>"
303-
" Other motions
304-
exe "nmap <silent> ".g:better_whitespace_operator." :<C-U>set opfunc=<SID>StripWhitespaceMotion<CR>g@"
305-
endif
299+
" Ensure we only map if no identical, user-defined mapping already exists
300+
if (empty(mapcheck(g:better_whitespace_operator, 'x')))
301+
" Visual mode
302+
exe "xmap <silent> ".g:better_whitespace_operator." :StripWhitespace<CR>"
303+
else
304+
call <SID>Echo("Whitespace operator not mapped for visual mode. Mapping already exists.")
305+
endif
306306

307+
" Ensure we only map if no identical, user-defined mapping already exists
308+
if (empty(mapcheck(g:better_whitespace_operator, 'n')))
309+
" Normal mode (+ space, with line count)
310+
exe "nmap <silent> ".g:better_whitespace_operator."<space> :<C-U>exe '.,+'.v:count' StripWhitespace'<CR>"
311+
" Other motions
312+
exe "nmap <silent> ".g:better_whitespace_operator." :<C-U>set opfunc=<SID>StripWhitespaceMotion<CR>g@"
313+
else
314+
call <SID>Echo("Whitespace operator not mapped for normal mode. Mapping already exists.")
315+
endif
316+
endif
307317

308318
" Process auto commands upon load, update local enabled on filetype change
309319
autocmd FileType * call <SID>ShouldSkipHighlight() | call <SID>SetupAutoCommands()

0 commit comments

Comments
 (0)