vim - show whitespace for selection only
Is it possible to show whitespace characters (space, tabs, newlines) only in visual mode, specifically in the selected text?
Or is there already a nice plugin that does the job?
Depending on how the highlighting is set up for visual mode, it is possible to set the foreground color of your white space characters to the background color of the window.
This is a sample Normal highlight group (taken from my own colorscheme) which defines the background color of the whole window:
hi Normal ctermbg=235 ctermfg=250 guibg=#262626 guifg=#bcbcbc cterm=NONE gui=NONE
The values we want are 235 for color terminals and #262626 for the GUI.
This is a sample Visual highlight group. It's only one foreground color on one background color:
hi Visual ctermbg=110 ctermfg=235 guibg=#8fafd7 guifg=#262626 cterm=NONE gui=NONE
The colors themselves don't really matter.
This is how we could set up the SpecialKey highlight group, using the values above:
hi SpecialKey ctermbg=NONE ctermfg=235 guibg=NONE guifg=#262626 cterm=NONE gui=NONE
And this is how it should look:
You'll need to edit your colorscheme for this to work across the board or to add this to your vimrc:
augroup colors autocmd! autocmd ColorScheme * hi SpecialKey ctermfg=235 guifg=#262626 autocmd ColorScheme * hi NonText ctermfg=235 guifg=#262626 augroup END
Of course, the colors above are are just an example; YMMV.
The "list" option in Vim will show whitespace characters:
List mode: Show tabs as CTRL-I is displayed, display $ after end of line. Useful to see the difference between tabs and spaces and for trailing blanks.
That being said...
This isn't exactly what you want, but Tim Pope's unimpaired plugin provides a very easy way to toggle off the "list" option (among many other options).
So, if you need to take a quick glance at those whitespace characters you can toggle them on/off with:
I consider unimpaired an essential, very light-weight plugin (basically just a set of mappings)
It is not possible, to permantly switch the display of the visual selection to enable the list mode, while the rest of the buffer does not have list set.
A workaround however is, after visually selecting some lines, press : so the command line looks like this: :'<,'> and then just use the :list command (which can be shortened to :l). So using :'<,'>l will echo the selected range of lines as if the list setting has been turned on. This however works only linewise, so it is not possible to only print the block selected region.
See the help at :h :l.