You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vim打开.py文件报错E28:无BadWhitespace高亮组,如何解决?

Hey there! Let's fix that E28 error you're seeing. The problem is straightforward: you're trying to use a highlight group called BadWhitespace in your autocommands, but you never actually defined that group in your .vimrc. Vim has no idea what styling to apply to that group, so it throws an error.

Step 1: Define the BadWhitespace highlight group

First, add a line to your .vimrc to create the BadWhitespace group and set its appearance. You can customize the colors to your liking—here's a common setup that makes trailing whitespace stand out with a red background:

" Define highlight group for bad whitespace
highlight BadWhitespace ctermbg=red guibg=red
  • ctermbg sets the background color for terminal Vim
  • guibg sets it for GUI Vim (like GVim)

Step 2: Fix your autocommands

Make sure this highlight group definition comes before the autocommands that reference it. Your Python whitespace-matching autocommands should look something like this:

" Mark trailing whitespace in Python files
autocmd BufRead,BufNewFile *.py match BadWhitespace /\s\+$/

" Optional: Mark extra spaces before commas (common Python style issue)
autocmd BufRead,BufNewFile *.py match BadWhitespace /\s\+\ze,/

Alternative: Use a built-in highlight group

If you don't want to create a custom group, you can use one of Vim's pre-defined highlight groups instead. For example, Error or Todo—just replace BadWhitespace with one of those:

autocmd BufRead,BufNewFile *.py match Error /\s\+$/

This avoids needing to define a custom group entirely, since Vim already knows what Error looks like.

Full working snippet for your .vimrc

Here's how the relevant section should look when put together, including the Python indent settings you mentioned:

" Python indentation setup
filetype plugin indent on
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=4

" Highlight bad whitespace
highlight BadWhitespace ctermbg=red guibg=red
autocmd BufRead,BufNewFile *.py match BadWhitespace /\s\+$/

Save your .vimrc, restart Vim, and open a Python file—your error should be gone, and trailing whitespace will be highlighted as expected!

内容的提问来源于stack exchange,提问作者Wet Feet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:13:24