Unix Vim处理BufRead自动命令时出现错误。

14

我是一个新的vim用户,正在跟随这个指南,使vim自动缩进python代码并标记不必要的空格:https://realpython.com/blog/python/vim-and-python-a-match-made-in-heaven/#vim-extensions

我的问题是当我在.py文件上启动vim时会收到如下错误提示:Error detected while processing BufRead Auto commands for "*.py": E28: No such highlight group name: BadWhitespace

这个错误我注释掉了以下代码:

" 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         

我该如何修复这个错误?这是我的完整 .vimrc 文件:

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
我遇到了相同的错误,跟着同样的页面。在处理多个命令(“set”集合)时,请不要忘记添加竖杠“|”。https://dev59.com/DVoV5IYBdhLWcg3wEbW-#36742908 - Scott Prive
我发现 NerdTree git 中的这个建议会导致错误。删除它可以解决问题。 - EsmaeelE
4个回答

31

有一个更简单的解决方案。你看,我也遇到了同样的问题,但通过这个小技巧,就没有错误消息了,一切都按照预期工作。

技巧是,BufNewFile和BufRead之间应该用空格隔开。

au BufNewFile, BufRead *.py

而不是 au BufNewFile,BufRead *.py 祝一切顺利


2
为什么vim的autocmd.txt文档中会有这个例子呢?:au BufNewFile,BufRead *.html so <sfile>:h/html.vim - Alexej Magura
嗯,我对此很好奇。如果我使用空格,它就无法正常工作。很难想象这在不同的版本中被更改了,因为这可能会导致破坏性变化。非常感谢您在这里提供见解。 - Aaron
1
测试过了,但并没有解决问题。 - yahan

7

该命令:

match BadWhitespace /\s\+$/

如果已定义BadWhitespace高亮组,则会突出显示尾随的空白字符。要检查是否已定义,请执行:highlight BadWhitespace。如果未定义,您可以使用默认的高亮组,例如:

match Cursor /\s\+$/

或者定义一个名为BadWhitespace的高亮组。一个可能的颜色组合是:
:highlight BadWhitespace ctermfg=16 ctermbg=253 guifg=#000000 guibg=#F8F8F0

在使用BadWhitespace的autocmd之前添加这一行代码。

似乎接近错误,但如果我想将高亮颜色更改为红色,该怎么办? - Wet Feet
然后你可以根据情况调整ctermbg的值。该值取决于你的终端。例如,通过执行:echo $TERM可以查看到我的终端是xterm-256color。然后我可以查阅一个xterm-256-color图表,发现红色对应的值是9。然后我设置ctermbg=9 - builder-7000
谢谢,不过你的代码中为什么有两个ctermfg和guifg值? - Wet Feet

6
问题出在多个 set 上。这一行中的第一个 set\ set tabstop=4 是命令,其后面的所有 set 都作为参数处理。
应该像这样写:
au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ softtabstop=4
    \ shiftwidth=4
    \ textwidth=79
    \ expandtab
    \ autoindent
    \ fileformat=unix

2

或者从这里获取:

" 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\+$/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接