- Notifications
You must be signed in to change notification settings - Fork81
Better whitespace highlighting for Vim
License
ntpeters/vim-better-whitespace
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This plugin causes all trailing whitespace characters (seeSupported WhitespaceCharacters below) to be highlighted. Whitespace for the current line willnot be highlighted while in insert mode. It is possible to disable current line highlighting while in othermodes as well (see options below). A helper function:StripWhitespace
is also provided to make whitespacecleaning painless.
Here is a screenshot of this plugin at work:
There are a few ways you can go about installing this plugin:
- If you haveVundle you can simply add:to your
Plugin'ntpeters/vim-better-whitespace'
.vimrc
file then run::PluginInstall
- If you are usingPathogen, you can just run the following command:
git clone git://github.com/ntpeters/vim-better-whitespace.git ~/.vim/bundle/vim-better-whitespace
- While this plugin can also be installed by copying its contents into your
~/.vim/
directory, I wouldhighly recommend using one of the above methods as they make managing your Vim plugins painless.
Whitespace highlighting is enabled by default, with a highlight color of red.
To set a custom highlight color, just call:
highlight ExtraWhitespace ctermbg=<desired_color>
or
letg:better_whitespace_ctermcolor='<desired_color>'
Similarly, to set gui color:
letg:better_whitespace_guicolor='<desired_color>'
To enable highlighting and stripping whitespace on save by default, use respectively
letg:better_whitespace_enabled=1letg:strip_whitespace_on_save=1
Set them to 0 to disable this default behaviour. See below for the blacklist of file typesand per-buffer settings.
To enable/disable/toggle whitespace highlighting in a buffer, call one of:
:EnableWhitespace:DisableWhitespace:ToggleWhitespace
The highlighting for the current line in normal mode can be disabled in two ways:
letg:current_line_whitespace_disabled_hard=1
This will maintain whitespace highlighting as it is, but may cause aslow down in Vim since it uses the CursorMoved event to detect andexclude the current line.
letg:current_line_whitespace_disabled_soft=1
This will use syntax based highlighting, so there shouldn't be aperformance hit like with the
hard
option. The drawback is that thishighlighting will have a lower priority and may be overwritten by higherpriority highlighting.
To clean extra whitespace, call:
:StripWhitespace
By default this operates on the entire file. To restrict the portion ofthe file that it cleans, either give it a range or select a group of linesin visual mode and then execute it.
There is an operator (defaulting to
<leader>s
) to clean whitespace.For example, in normal mode,<leader>sip
will remove trailing whitespace from thecurrent paragraph.You can change the operator it, for example to set it to _s, using:
letg:better_whitespace_operator='_s'
Now
<number>_s<space>
strips whitespace on <number> lines, and_s<motion>
on thelines affected by the motion given. Set to the empty string to deactivate the operator.Note: This operator will not be mapped if an existing, user-definedmapping is detected for the provided operator value.
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.
If you want this behaviour by default for all filetypes, add the following to your
~/.vimrc
:letg:strip_whitespace_on_save=1
For exceptions of all see
g:better_whitespace_filetypes_blacklist
.If you would prefer to only strip whitespace for certain filetypes, addthe following to your
~/.vimrc
:autocmdFileType<desired_filetypes> EnableStripWhitespaceOnSave
where
<desired_filetypes>
is a comma separated list of the file types you wantto be stripped of whitespace on file save ( ie.javascript,c,cpp,java,html,ruby
)If you want to disable automatically stripping whitespace for large files, you can specifya maximum number of lines (e.g. 1000) by adding the following to your
~/.vimrc
:letg:strip_max_file_size=1000
This overrides
let g:strip_whitespace_on_save
but not:EnableStripWhitespaceOnSave
.Set to0
to deactivate.By default, you will be asked for confirmation before whitespace isstripped when you save the file. This can be disabled by adding thefollowing to your
~/.vimrc
:let g:strip_whitespace_confirm=0
By default, all the lines in the file will have their trailing whitespace strippedwhen you save the file. This can be changed to only the modified lines, by addingthe following to your
~/.vimrc
:let g:strip_only_modified_lines=1
You can override the binary used to check which lines have been modified in the file.For example to force a 'diff' installed in a different prefix and ignoring the changesdue to tab expansions, you can set the following:
let g:diff_binary='/usr/local/bin/diff -E'
To disable the highlighting for specific file types, add the following to your
~/.vimrc
:letg:better_whitespace_filetypes_blacklist=['<filetype1>','<filetype2>','<etc>']
This replaces the filetypes from the default list of blacklisted filetypes. Thedefault types that are blacklisted are:
['diff','git','gitcommit','unite','qf','help','markdown','fugitive']
If you prefer to also keep these default filetypes ignored, simply include them in theblacklist:
letg:better_whitespace_filetypes_blacklist=['<filetype1>','<filetype2>','<etc>','diff','git','gitcommit','unite','qf','help','markdown','fugitive']
This blacklist can be overriden on a per-buffer basis using the buffer toggle enable anddisable commands presented above. For example:
" highlight whitespace in markdown files, though stripping remains disabled by the blacklist:autocmdFileTypemarkdown EnableWhitespace" Do not modify kernel files, even though their type is not blacklisted and highlighting is enabled:autocmdBufRead/usr/src/linux* DisableStripWhitespaceOnSave
To strip white lines at the end of the file when stripping whitespace, set this option in your
.vimrc
:letg:strip_whitelines_at_eof=1
To highlight space characters that appear before or in-between tabs, add the following to your
.vimrc
:letg:show_spaces_that_precede_tabs=1
Such spaces cannot 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 fixthem manually.To ignore lines that contain only whitespace, set the following in your
.vimrc
:letg:better_whitespace_skip_empty_lines=1
To navigate to the previous or next trailing whitespace, you can use commands that youcan map thusly in your
.vimrc
:nnoremap]w:NextTrailingWhitespace<CR>nnoremap[w:PrevTrailingWhitespace<CR>
Note: those command take an optional range as argument, so you can for example select sometext in visual mode and search only inside it:
:'<,'>NextTrailingWhitespace
To enable verbose output for each command, set verbosity in your
.vimrc
:letg:better_whitespace_verbosity=1
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 ofhorizontal whitespace characters to match for both highlighting and stripping.
This is list should match against all ASCII and Unicode horizontal whitespacecharacters:
U+0009 TAB U+0020 SPACE U+00A0 NO-BREAK SPACE U+1680 OGHAM SPACE MARK U+180E MONGOLIAN VOWEL SEPARATOR U+2000 EN QUAD U+2001 EM QUAD U+2002 EN SPACE U+2003 EM SPACE U+2004 THREE-PER-EM SPACE U+2005 FOUR-PER-EM SPACE U+2006 SIX-PER-EM SPACE U+2007 FIGURE SPACE U+2008 PUNCTUATION SPACE U+2009 THIN SPACE U+200A HAIR SPACE U+200B ZERO WIDTH SPACE U+202F NARROW NO-BREAK SPACE U+205F MEDIUM MATHEMATICAL SPACE U+3000 IDEOGRAPHIC SPACE U+FEFF ZERO WIDTH NO-BREAK SPACE
A file is provided with samples of each of these characters to check the pluginworking with them: whitespace_examples.txt
If you encounter any additional whitespace characters I have missed here,please submit a pull request.
Here are a couple more screenshots of the plugin at work.
This screenshot shows the current line not being highlighted in insert mode:
This screenshot shows the current line not being highlighted in normal mode(CurrentLineWhitespaceOff hard
):
This screenshot shows that highlighting works fine for spaces, tabs, and a mixture of both:
Hopefully some of the most common questions will be answered here. If you still have a questionthat I have failed to address, please open an issue and ask it!
Q: Why is trailing whitespace such a big deal?
A: In most cases it is not a syntactical issue, but rather is a common annoyance amongprogrammers.
Q: Why not just uselistchars
withSpecialKey
highlighting?
A: I tried usinglistchars
to show trail characters withSpecialKey
highlighting applied.Using this method the characters would still show on the current line for me even when theSpecialKey
foreground highlight matched theCursorLine
background highlight.
Q: Okay, solistchars
doesn't do exactly what you want, why not just use amatch
in yourvimrc
?
A: I am usingmatch
in this plugin, but I've also added a way to exclude the current line ininsert mode and/or normal mode.
Q: If you just want to exclude the current line, why not just use syntax-based highlight ratherthan usingmatch
andCursorMoved
events?
A: Syntax-based highlighting is an option in this plugin. It is used to omit the current line whenusingCurrentLineWhitespaceOff soft
. The only issue with this method is thatmatch
highlighingtakes higher priorty than syntax highlighting. For example, when using a plugin such asIndent Guides, syntax-based highlighting ofextra whitespace will not highlight additional white space on emtpy lines.
Q: I already have my own method of removing white space, why is the method used in this plugin better?
A: It may not be, depending on the method you are using. The method used in this plugin strips extrawhite space and then restores the cursor position and last search history.
Q: Most of this is pretty easy to just add to users'vimrc
files. Why make it a plugin?
A: It is true that a large part of this is fairly simple to make a part of an individualsconfiguration in theirvimrc
. I wanted to provide something that is easy to setup and usefor both those new to Vim and others who don't want to mess around setting up thisfunctionality in theirvimrc
.
Q: Can you add indentation highlighting for spaces/tabs? Can you add highlighting for othertypes of white space?
A: No, and no. Sorry, but both are outside the scope of this plugin. The purpose of this pluginis to provide a better experience for showing and dealing with extra white space. There is already anamazing plugin for showing indentation in Vim calledIndentGuides. For other types of white space highlighting,listchars should be sufficient.
Q: I have a better way to do something in this plugin. OR You're doing something stupid/wrong/bad.
A: If you know of a better way to do something I am attempting in this plugin, or if I am doingsomething improperly/not reccomended then let me know! Please either open an issue informingme or make the changes yourself and open a pull request. If I am doing something that is bador can be improved, I am more than willing to hear about it!
Toggling the current line whitespace mode is now a plugin configuration,and can not be done dynamically anymore. Thus the folowing commands are now deprecated:
:CurrentLineWhitespaceOff<level>
where<level>
is eitherhard
orsoft
, and:
:CurrentLineWhitespaceOn
If you really miss this feature, its withdrawal can easily be overridenby adding the following to the vimrc (after loading the plugin initially):
fun!BetterWhitespaceCurrentLineMode(type)" set setting to whatever was passedletg:current_line_whitespace_disabled_soft=a:type=='soft'letg:current_line_whitespace_disabled_hard=a:type=='hard'" reload pluginunlet!g:loaded_better_whitespace_pluginruntimeplugin/better-whitespace.vim" Re-override the deprecated commandscommand! -nargs=1 CurrentLineWhitespaceOffcallBetterWhitespaceCurrentLineMode(<f-args>)command! CurrentLineWhitespaceOncallBetterWhitespaceCurrentLineMode('off')" Manually trigger change for current buffer." BufWinEnter will take care of the rest.filetypedetectendfun" Override deprecated commands, after (!) loading plugincommand! -nargs=1 CurrentLineWhitespaceOffcallBetterWhitespaceCurrentLineMode(<f-args>)command! CurrentLineWhitespaceOncallBetterWhitespaceCurrentLineMode('off')
If you like this plugin, please star it on Github and vote it up at Vim.org!
Repository exists at:http://github.com/ntpeters/vim-better-whitespace
Plugin also hosted at:http://www.vim.org/scripts/script.php?script_id=4859
Originally inspired by:https://github.com/bronson/vim-trailing-whitespace
Based on:
http://sartak.org/2011/03/end-of-line-whitespace-in-vim.html
About
Better whitespace highlighting for Vim
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.