Skip to content

Commit 8f488a4

Browse files
Cimbalintpeters
authored andcommitted
Fix various issues/PRs and update documentation (#72)
* Fix typo in README * Buffer-local stripping of whitespace * Add option to skip whitespace only lines, closes #65 * Add functions that jump to trailing white space Closes #32 * Fix typo: Hightlight->Highlight * A few more typo corrections. * More typos * Add option to strip white lines at EOF, closes #67 * Add operator for StripWhitespace (defaults to space) Closes #18 * 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. * Do not strip spaces before tabs Only match them, see discussion on #58 * Update README and complete examples
1 parent 688837f commit 8f488a4

File tree

4 files changed

+218
-52
lines changed

4 files changed

+218
-52
lines changed

README.md

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Vim Better Whitespace Plugin
22

3-
This plugin causes all trailing whitespace characters (see [Supported Whitespace Characters](#supported-whitespace-characters) below) to be
4-
highlighted. Whitespace for the current line will not be highlighted
5-
while in insert mode. It is possible to disable current line highlighting while in other
6-
modes as well (see options below). A helper function `:StripWhitespace` is also provided
7-
to make whitespace cleaning painless.
3+
This plugin causes all trailing whitespace characters (see [Supported Whitespace
4+
Characters](#supported-whitespace-characters) below) to be highlighted. Whitespace for the current line will
5+
not be highlighted while in insert mode. It is possible to disable current line highlighting while in other
6+
modes as well (see options below). A helper function `:StripWhitespace` is also provided to make whitespace
7+
cleaning painless.
88

99
Here is a screenshot of this plugin at work:
1010
![Example Screenshot](http://i.imgur.com/St7yHth.png)
@@ -13,34 +13,45 @@ Here is a screenshot of this plugin at work:
1313
There are a few ways you can go about installing this plugin:
1414

1515
1. If you have [Vundle](https://github.com/gmarik/Vundle.vim) you can simply add:
16-
```
16+
```vim
1717
Plugin 'ntpeters/vim-better-whitespace'
1818
```
1919
to your `.vimrc` file then run:
20-
```
20+
```vim
2121
:PluginInstall
2222
```
2323
2. If you are using [Pathogen](https://github.com/tpope/vim-pathogen), you can just run the following command:
2424
```
2525
git clone git://github.com/ntpeters/vim-better-whitespace.git ~/.vim/bundle/vim-better-whitespace
2626
```
27-
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.
27+
3. While this plugin can also be installed by copying its contents into your `~/.vim/` directory, I would
28+
highly recommend using one of the above methods as they make managing your Vim plugins painless.
2829
2930
## Usage
3031
Whitespace highlighting is enabled by default, with a highlight color of red.
3132
3233
* To set a custom highlight color, just call:
33-
```
34+
```vim
3435
highlight ExtraWhitespace ctermbg=<desired_color>
3536
```
3637
37-
* To toggle whitespace highlighting on/off, call:
38+
* To enable highlighting and stripping whitespace on save by default, use respectively
39+
```vim
40+
let g:better_whitespace_enabled=1
41+
let g:strip_whitespace_on_save=1
3842
```
43+
Set them to 0 to disable this default behaviour. See below for the blacklist of file types
44+
and per-buffer settings.
45+
46+
* To enable/disable/toggle whitespace highlighting in a buffer, call one of:
47+
```vim
48+
:EnableWhitespace
49+
:DisableWhitespace
3950
:ToggleWhitespace
4051
```
4152
4253
* To disable highlighting for the current line in normal mode call:
43-
```
54+
```vim
4455
:CurrentLineWhitespaceOff <level>
4556
```
4657
Where `<level>` is either `hard` or `soft`.
@@ -55,69 +66,121 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
5566
priority highlighting.
5667
5768
* To re-enable highlighting for the current line in normal mode:
58-
```
69+
```vim
5970
:CurrentLineWhitespaceOn
6071
```
6172
6273
* To clean extra whitespace, call:
63-
```
74+
```vim
6475
:StripWhitespace
6576
```
6677
By default this operates on the entire file. To restrict the portion of
6778
the file that it cleans, either give it a range or select a group of lines
6879
in visual mode and then execute it.
6980
70-
* To enable/disable stripping of extra whitespace on file save, call:
71-
```
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
83+
current paragraph.
84+
85+
You can change the operator it, for example to set it to _s, using:
86+
```vim
87+
let g:better_whitespace_operator='_s'
88+
```
89+
Now `<number>_s<space>` strips whitespace on \<number\> lines, and `_s<motion>` on the
90+
lines affected by the motion given. Set to the empty string to deactivate the operator.
91+
92+
* To enable/disable stripping of extra whitespace on file save for a buffer, call one of:
93+
```vim
94+
:EnableStripWhitespaceOnSave
95+
:DisableStripWhitespaceOnSave
7296
:ToggleStripWhitespaceOnSave
7397
```
7498
This will strip all trailing whitespace everytime you save the file for all file types.
7599
76100
* If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`:
77101
78-
```
79-
autocmd BufEnter * EnableStripWhitespaceOnSave
102+
```vim
103+
let g:strip_whitespace_on_save = 1
80104
```
81105
82106
For exceptions of all see ```g:better_whitespace_filetypes_blacklist```.
83107
84108
* If you would prefer to only strip whitespace for certain filetypes, add
85109
the following to your `~/.vimrc`:
86110
87-
```
88-
autocmd FileType <desired_filetypes> autocmd BufEnter <buffer> EnableStripWhitespaceOnSave
111+
```vim
112+
autocmd FileType <desired_filetypes> EnableStripWhitespaceOnSave
89113
```
90114
91115
where `<desired_filetypes>` is a comma separated list of the file types you want
92116
to be stripped of whitespace on file save ( ie. `javascript,c,cpp,java,html,ruby` )
93-
Note that `<buffer>` is a keyword here denoting operation on the current buffer and
94-
should stay just as it appears in the line above.
95117
96118
* To disable this plugin for specific file types, add the following to your `~/.vimrc`:
97-
```
119+
```vim
98120
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>']
99121
```
100122
This replaces the filetypes from the default list of blacklisted filetypes. The
101123
default types that are blacklisted are:
102-
```
124+
```vim
103125
['diff', 'gitcommit', 'unite', 'qf', 'help', 'markdown']
104126
```
105127
If you do not want any of these filetypes unignored, simply include them in the
106128
blacklist:
107-
```
129+
```vim
108130
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>',
109131
'diff', 'gitcommit', 'unite', 'qf', 'help']
110132
```
111133
112-
* To enable verbose output for each command, set verbosity in your `.vimrc`:
134+
This blacklist can be overriden on a per-buffer basis using the buffer toggle enable and
135+
disable commands presented above. For example:
136+
```vim
137+
" highlight whitespace in markdown files, though stripping remains disabled by the blacklist
138+
:autocmd FileType markdown EnableWhitespace
139+
" Do not modify kernel files, even though their type is not blacklisted and highlighting is enabled
140+
:autocmd BufRead /usr/src/linux* DisableStripWhitespaceOnSave
141+
```
142+
143+
* To strip white lines at the end of the file when stripping whitespace, set this option in your `.vimrc`:
144+
```vim
145+
let g:strip_whitelines_at_eof=1
146+
```
147+
148+
* To highlight space characters that appear before or in-between tabs, add the following to your `.vimrc`:
149+
```vim
150+
let g:show_spaces_that_precede_tabs=1
113151
```
152+
Such spaces can **not** be automatically removed by this plugin, though you can try
153+
[`=` to fix indentation](http://vimdoc.sourceforge.net/htmldoc/change.html#=).
154+
You can still navigate to the highlighted spaces with Next/PrevTrailingWhitespace (see below), and fix
155+
them manually.
156+
157+
* To ignore lines that contain only whitespace, set the following in your `.vimrc`:
158+
```vim
159+
let g:better_whitespace_skip_empty_lines=1
160+
```
161+
162+
* To navigate to the previous or next trailing whitespace, you can use commands that you
163+
can map thusly in your `.vimrc`:
164+
```vim
165+
nnoremap ]w :NextTrailingWhitespace<CR>
166+
nnoremap [w :PrevTrailingWhitespace<CR>
167+
```
168+
Note: those command take an optional range as argument, so you can for example select some
169+
text in visual mode and search only inside it:
170+
```vim
171+
:'<,'>NextTrailingWhitespace
172+
```
173+
174+
* To enable verbose output for each command, set verbosity in your `.vimrc`:
175+
```vim
114176
let g:better_whitespace_verbosity=1
115177
```
116178
179+
117180
## Supported Whitespace Characters
118181
Due to the fact that the built-in whitespace character class for patterns (`\s`)
119182
only matches against tabs and spaces, this plugin defines its own list of
120-
horizontal whitepsace characters to match for both highlighting and stripping.
183+
horizontal whitespace characters to match for both highlighting and stripping.
121184
122185
This is list should match against all ASCII and Unicode horizontal whitespace
123186
characters:
@@ -157,7 +220,7 @@ Here are a couple more screenshots of the plugin at work.
157220
This screenshot shows the current line not being highlighted in insert mode:
158221
![Insert Screenthot](http://i.imgur.com/RNHR9KX.png)
159222
160-
This screenshot shows the current line not being highlighted in normal mode( `CurrentLineWhitespaceOff hard` ):
223+
This screenshot shows the current line not being highlighted in normal mode(`CurrentLineWhitespaceOff hard`):
161224
![Normal Screenshot](http://i.imgur.com/o888Z7b.png)
162225
163226
This screenshot shows that highlighting works fine for spaces, tabs, and a mixture of both:
@@ -212,17 +275,18 @@ A: It is true that a large part of this is fairly simple to make a part of an i
212275
**Q: Can you add indentation highlighting for spaces/tabs? Can you add highlighting for other
213276
types of white space?**
214277
215-
A: No, and no. Sorry, but both are outside the scope of this plugin. The purpose of this plugin
216-
is to provide a better experience for showing and dealing with extra white space. There is
217-
already an amazing plugin for showing indentation in Vim called [Indent Guides](https://github.com/nathanaelkane/vim-indent-guides).
218-
For other types of white space highlighting, [listchars](http://vimdoc.sourceforge.net/htmldoc/options.html#'listchars') should be sufficient.
278+
A: No, and no. Sorry, but both are outside the scope of this plugin. The purpose of this plugin
279+
is to provide a better experience for showing and dealing with extra white space. There is already an
280+
amazing plugin for showing indentation in Vim called [Indent
281+
Guides](https://github.com/nathanaelkane/vim-indent-guides). For other types of white space highlighting,
282+
[listchars](http://vimdoc.sourceforge.net/htmldoc/options.html#'listchars') should be sufficient.
219283
220284
**Q: I have a better way to do something in this plugin. OR You're doing something stupid/wrong/bad.**
221285
222286
A: If you know of a better way to do something I am attempting in this plugin, or if I am doing
223287
something improperly/not reccomended then let me know! Please either open an issue informing
224288
me or make the changes yourself and open a pull request. If I am doing something that is bad
225-
or can be improved, I more than willing to hear about it!
289+
or can be improved, I am more than willing to hear about it!
226290
227291
## Promotion
228292
If you like this plugin, please star it on Github and vote it up at Vim.org!

doc/better-whitespace.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This enables/disables stripping of extra whitespace on file save.
3838
*CurrentLineWhitespaceOff*
3939

4040
Calling :CurrentLineWhitespaceOff with the option either 'hard' or 'soft' will
41-
disable whitespace highlighting for the currentline.
41+
disable whitespace highlighting for the current line.
4242

4343
If 'hard' option is used, then all highlighting remains the same except that
4444
the current line is not highlighted.
@@ -54,7 +54,7 @@ this option.
5454
*CurrentLineWhitespaceOn
5555

5656
Call :CurrentLineWhitespaceOn to enable whitespace highlighting for the current
57-
line. Highlighting is still disabled for the current line while in inserte mode.
57+
line. Highlighting is still disabled for the current line while in insert mode.
5858

5959
Repository exists at: http://github.com/ntpeters/vim-better-whitespace
6060

0 commit comments

Comments
 (0)