I am trying to customize the syntax highlighting for python in vim. I want to highlight the keyword self
but only when it is followed by a .
. Here is the code I came up with:
syn match pythonBoolean "\(\Wself\)\%(\.\)"
Unfortunately, the .
is also highlighted though I use a non capturing group \%(\.\)
.
Any idea?
You need to use the lookaround:
:syn match pythonBoolean "\(\W\|^\)\zsself\ze\."
or
:syn match pythonBoolean "\(\W\|^\)\@<=self\(\.\)\@="
Building on @Meninx's answer, I added this to my .vimrc
:
augroup PythonCustomization
" highlight python self, when followed by a comma, a period or a parenth
:autocmd FileType python syn match pythonStatement "\(\W\|^\)\@<=self\([\.,)]\)\@="
augroup END
Note 1: that in addition to what the op asked, it will also highlight when self
is followed by a comma or a closing parenthesis.
Note 2: instead of using pythonBoolean
, this highlights self
using pythonStatement
(personal preference). You can use other highlight-groups (run :syn
with a python file open to see what's available)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With