15

I am a new vim user following this guide to make vim indent python code automatically and flag unnecssary whitespace: https://realpython.com/blog/python/vim-and-python-a-match-made-in-heaven/#vim-extensions

The issue I have is receiving this error when I start up vim on a .py file: Error detected while processing BufRead Auto commands for "*.py": E28: No such highlight group name: BadWhitespace

This error I comment out the following lines:

" Flag unnecessary whitespace                                           
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/   <- this line   

" UTF8 Support                                                          
set encoding=utf-8                                                      
" Proper PEP8 Identation                                                
au BufNewFile,BufRead *.py                                              
    \ set tabstop=4                                                     
"    \ set softtabstop=4      <-- this line                                          
    \ set shiftwidth=4                                                  
    \ set textwidth=79                                                  
    \ set expandtab                                                     
    \ set autoindent                                                    
    \ set fileformat=unix         

How can I fix this error? This is my complete .vimrc file:

set nocompatible              " required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=/home/frank/.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'

" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)
Plugin 'tmhedberg/SimpylFold'
Plugin 'vim-scripts/indentpython.vim'
Bundle 'Valloric/YouCompleteMe'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}
" ...

" 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>

" Enable folding
set foldmethod=indent
set foldlevel=99
" Enable folding with the spacebar
nnoremap <space> za
" See docstrings for folded code
let g:SimpylFold_docstring_preview=1

" Flag unnecessary whitespace
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/      <-this line

" UTF8 Support
set encoding=utf-8
" Proper PEP8 Identation        <-this line
au BufNewFile,BufRead *.py
    \ set tabstop=4
"    \ set softtabstop=4 
    \ set shiftwidth=4
    \ set textwidth=79
    \ set expandtab
    \ set autoindent
    \ set fileformat=unix

" For Full stack development 'au' command
"au BufNewFile,BufRead *.js, *.html, *.css
"    \ set tabstop=2
"    \ set softtabstop=2
"    \ set shiftwidth=2

" YouCompleteMe plugin customization
let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>

"python with virtualenv support
py << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))
EOF

" Makes python code pretty
let python_highlight_all=1
syntax on

" Adds a bit of logic to define which color scheme to use based upon VIM mode
if has('gui_running')
  set background=dark
  colorscheme solarized
else
  colorscheme zenburn
endif

" Press F5 to toggle between dark and light theme
call togglebg#map("<F5>")

" Hybrid line numbers
:set number relativenumber
:augroup numbertoggle
:  autocmd!
:  autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
:  autocmd BufLeave,FocusLost,InsertEnter   * set norelativenumber
:augroup END

set pastetoggle=<F10>
2
  • 2
    I had the same error, following the SAME page. Don't forget to add a bar | when dealing with multiple commands (the collection of "set"s). stackoverflow.com/questions/36741450/… Commented Jan 8, 2019 at 15:47
  • I find that this recommendation in NerdTree git cause error. remove it to solve problem. Commented Jul 18, 2021 at 6:08

4 Answers 4

34

There is a simpler solution. You see I had ran into same problem and after this little trick there was no error message and everything is working as it should.

Trick is, BufNewFile,BufRead should be seperated by a space. So the line should look like

au BufNewFile, BufRead *.py

and not au BufNewFile,BufRead *.py best regards

Sign up to request clarification or add additional context in comments.

3 Comments

Why does the vim autocmd.txt documentation have this example in it then? :au BufNewFile,BufRead *.html so <sfile>:h/html.vim
hmm, I'm curious about this. I cannot use a space or it does not work properly for me. It's hard to imagine that was changed in a different version, as it would have breaking changes. Appreciate insights here.
tested this and doesn't solve the problem
7

The command:

match BadWhitespace /\s\+$/

will highlight trailing white spaces provided that BadWhitespace highlighting group is defined. To check that it is defined do :highlight BadWhitespace. If it is not defined you can either use a default highlighting group, for example:

match Cursor /\s\+$/

or define a BadWhitespace highlighting group. A possible color combination is:

:highlight BadWhitespace ctermfg=16 ctermbg=253 guifg=#000000 guibg=#F8F8F0

Add this line of code before the autocmd that uses BadWhitespace.

3 Comments

Seems to be close to the error, however what should I do if I want to change the color of the highlighting to red?
Then you can adjust the value ctermbg accordingly. The value will depend on your terminal. E.g. doing :echo $TERM shows that my terminal is xterm-256color. Then I can look up a xterm-256-color chart and find that red is 9. Then I set ctermbg=9.
Thank you, however, why are there two ctermfg and guifg values in your code?
6

The problem is the multiple sets. The first set from this line:

\ set tabstop=4

is the command, and all the following sets are treated as arguments.

It should look like this:

au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ softtabstop=4
    \ shiftwidth=4
    \ textwidth=79
    \ expandtab
    \ autoindent
    \ fileformat=unix

Comments

2

Alternatively from here:

" Use the below highlight group when displaying bad whitespace is desired.
highlight BadWhitespace ctermbg=red guibg=red

" Display tabs at the beginning of a line in Python mode as bad.
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/

" Make trailing whitespace be flagged as bad.
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.