Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Vim highlighting trailing whitespace in python files

Tags:

python

vim

Title of the question pretty much describes the problem

In my vimrc file i even typed this:

let python_highlight_all = 0

But this does not help at all

UPD: I got it to work with monkeypatch in python.vim. I just commented this line:

syn match   pythonSpaceError    display excludenl "\s\+$"

If someone has a better solution please answer

like image 600
jealrockone Avatar asked Sep 07 '25 11:09

jealrockone


2 Answers

Here are at least 2 possible reasons for this:

  1. python_highlight_all is defined
  2. python_space_error_highlight is defined

Since the problem is gone if you monkeypatch syntax/python.vim this highlight should be coming from Vim's default Python syntax file. In the syntax file, the highlight is controlled by the code below:

# vim80/syntax/python.vim
if exists("python_highlight_all")
  ...
  let python_space_error_highlight = 1
endif

With this code, python_space_error_highlight is enabled if a variable python_highlight_all exists regardless of its value. So probably python_highlight_all and/or python_space_error_highlight is defined somewhere.

How's the result if you add the code below to your vimrc?

if exists('python_highlight_all')
    unlet python_highlight_all
endif
if exists('python_space_error_highlight')
    unlet python_space_error_highlight
endif
like image 107
Tacahiroy Avatar answered Sep 09 '25 18:09

Tacahiroy


In $VIMRUNTIME/syntax/python.vim, the pythonSpaceError syntax group is surrounded by an if exists("python_space_error_highlight") conditional (in Vim 8.0; using a syntax file from 2016 Oct 29).

So, there's no need to monkey-patch, you can indeed turn this off, by defining neither python_space_error_highlight nor python_highlight_all (because the latter definition will automatically define the former, too).

(Even when a config value is 0, as the script just tests for the existence of the variable (which is odd and against the usual convention; you may want to complain about this to the script's author). Therefore, ensure that you don't have of those config variables set, and it should work.

like image 45
Ingo Karkat Avatar answered Sep 09 '25 19:09

Ingo Karkat