2

Following Vim highlighting with solarized color scheme, I tried this

" Default color scheme
syntax enable
 set background=dark
colorscheme solarized
autocmd ColorScheme * highlight RedundantSpaces ctermbg=red
match RedundantSpaces /\s\+$/

However I am still unable to get my whitespace to show up. Here's my .vimrc:

set nocompatible              " required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

Plugin 'tmhedberg/SimpylFold'
Plugin 'Vimjas/vim-python-pep8-indent'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jeffkreeftmeijer/vim-numbertoggle'
Plugin 'altercation/vim-colors-solarized'

" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)

" ...

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

"split navigations
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

" See docstrings for folded code
let g:SimpylFold_docstring_preview=1
" Enable folding
set foldmethod=indent
set foldlevel=99

" Enable folding with the spacebar
nnoremap <space> za

" UTF8 Support
set encoding=utf-8

" Syntastic recommended settings
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

" Default color scheme
syntax enable
set background=dark
colorscheme solarized
autocmd ColorScheme * highlight RedundantSpaces ctermbg=red
match RedundantSpaces /\s\+$/

" Make my code look pretty
let python_highlight_all=1
syntax on

" line numbering
set number relativenumber

Also, if possible, how do I use https://github.com/vim-scripts/ShowTrailingWhitespace with Solarize?

Wet Feet
  • 4,435
  • 10
  • 28
  • 41
  • 1
    why do you want this in an autocmd? Does it work if you just type in vim's command line `:highlight RedundantSpaces ctermbg=red guibg=red | match RedundantSpaces /\s\+$/`? – sudavid4 Feb 22 '18 at 20:17
  • About this plugin, doesn't it just work? Does it work without the solarized theme? If yes, does the order in which you load the plugins matter (try to swap the order of them inside the `vunde#begin` `vundle#end`) – sudavid4 Feb 22 '18 at 20:23
  • yes typing the command works, however I want vim to automatically highlight trailing whitespaces without having to type a command. Swapping the order in which plugins are used did not change anything. Trailing Whitespaces are only highlighted when my cursor is at the whitespace itself. – Wet Feet Feb 23 '18 at 14:01

3 Answers3

15

add this to the very bottom of your .vimrc

highlight RedundantSpaces ctermbg=red guibg=red 
match RedundantSpaces /\s\+$/

and you should be good to go, no need for ShowTrailingWhitespace plugin

sudavid4
  • 1,081
  • 6
  • 14
  • Thank you, not using the plugin + placing this at the bottom fixed it for me! – Wet Feet Feb 23 '18 at 18:28
  • This seems to disable the syntax highlighter in newer vim versions. – Durand Jul 15 '20 at 14:28
  • In Markdown files two trailing spaces is an allowed token to force a line break. Is there a way to disable this specifically for `.md` files for lines which have exactly two trailing spaces (or change the highlighting to a different color like green)? – lanoxx Nov 27 '20 at 09:11
  • why put in the middle in my .vimrc doesn't work, but put in the buttom work – yuanjianpeng Feb 17 '22 at 02:41
  • @yuanjianpeng if I remember correctly the reason to put it in the very end is because some plugin might override this, having it in the very end of your vimrc makes it that you override the plugin configuration and not the other way around. Take this with a grain of salt though as I'm not 100% sure that this explanation is correct. – sudavid4 Mar 23 '22 at 16:05
  • You can reuse the colours assigned to Error by linking the new match to Error, rather than manually setting the colour: `highlight default link RedundantSpaces Error` `match RedundantSpaces /\s\+$/` – ljden Jul 11 '22 at 08:02
3

There's no need to use a plugin just to trail whitespaces, i have this in my .vimrc:

autocmd BufWinEnter <buffer> match Error /\s\+$/
autocmd InsertEnter <buffer> match Error /\s\+\%#\@<!$/
autocmd InsertLeave <buffer> match Error /\s\+$/
autocmd BufWinLeave <buffer> call clearmatches()

It trails the whitespaces while you are editing the code, so i think that it will do what you want.

Valeyard
  • 31
  • 2
  • I like this solution better than the accepted answer, since it uses `match Error` which uses the current color scheme among other things. – Freggar Oct 10 '19 at 12:22
  • @Freggar You can also link the RedundantSpaces match to Error so they share the same colours – ljden Jul 11 '22 at 08:12
0

Here's an excerpt from my ~/.vimrc that highlights redundant trailing whitespace and spaces before tabs. It takes care of the specifics of the insert mode, and also handles automatically generated files, such as the git commit messages, in a special way that keeps their contents readable.

" Highlight trailing whitespace and spaces before tabs, but ignore
" harmless leading and trailing spaces found at the start of lines
" in certain types of automatically generated files
"
function! IgnoreWhitespace()
  if index(["diff", "gitcommit", "gitsendemail"], &filetype) >= 0
    2match IgnoredWhitespace /^ $\|^ \ze\t\|^#\zs $/
  endif
endfunction

highlight TrailingWhitespace ctermbg=13
highlight IgnoredWhitespace  ctermbg=53

augroup vimrc-trailing-whitespace
  autocmd!
  autocmd BufWinEnter * match TrailingWhitespace /\s\+$\| \+\ze\t/
  autocmd BufWinEnter * call  IgnoreWhitespace()
  autocmd InsertEnter * match TrailingWhitespace /\s\+\%#\@<!$\| \+\ze\t/
  autocmd InsertEnter * call  IgnoreWhitespace()
  autocmd InsertLeave * match TrailingWhitespace /\s\+$\| \+\ze\t/
  autocmd InsertLeave * call  IgnoreWhitespace()
  autocmd BufWinLeave * call  clearmatches()
augroup END

Of course, you can adjust the highlight colors in the vimscript code above to fit your taste. My goal was not to make the so-called harmless leading and trailing spaces completely invisible, but to have them kind of ignored by making them barely visible, so I can still see such spaces in an unobtrusive way.

Here's a screenshot that shows an example of a diff file type generated by git add -p and displayed in vim with this vimcode in effect. The test file also contains some artificially injected whitespace, which tests the vimscript code additionally and showcases its visual effects.

Redundant trailing whitespace highlighted

dsimic
  • 23
  • 5