I’m working on a custom Vim colorscheme (Monokai-inspired with a Gruvbox background, 256-color accurate) and I’d like to highlight Python class names in a different color. I'm using Vim macOS version - arm64.
Here’s what I’ve tried so far in my colors/cloud.vim:
" Python specific
hi pythonFunction ctermfg=118 cterm=bold
hi pythonBuiltin ctermfg=81 cterm=bold
hi pythonSelf ctermfg=161 cterm=italic
" Function and class name highlighting
autocmd FileType python syntax match pythonFunctionName /\<\h\w*\ze\s*(/
autocmd FileType python hi pythonFunctionName ctermfg=118 cterm=bold
autocmd FileType python syntax match pythonClassName /\<class\s\+\zs\h\w*/
autocmd FileType python hi pythonClassName ctermfg=81 cterm=bold
The function highlighting (pythonFunctionName) works fine, but the class name highlighting does not show up at all. For example:
class MyExample:
def do_something(self):
pass
Here, MyExample doesn’t get highlighted differently, even though my regex looks like it should catch it.
I’ve tried clearing/redefining pythonStatement and related groups, but no luck.
How can I properly highlight class names in Python files in Vim when using a custom colorscheme?
Do I need to contains / contained the match somewhere, or is there a better way to hook into Python syntax groups?
My full file is as follow:
" Monokai (Python-focused) with Gruvbox background - 256 colors accurate
hi clear
set background=dark
if version > 580
hi clear
if exists("syntax_on")
syntax reset
endif
endif
let g:colors_name="claude"
" Base UI (cterm)
hi Normal ctermfg=188 ctermbg=235
hi Comment ctermfg=244 cterm=italic
hi CursorLine ctermbg=237
hi CursorColumn ctermbg=237
hi LineNr ctermfg=241 ctermbg=235
hi NonText ctermfg=239 ctermbg=235
hi VertSplit ctermfg=239 ctermbg=235
hi StatusLine ctermfg=188 ctermbg=239 cterm=bold
hi StatusLineNC ctermfg=244 ctermbg=237
hi Visual ctermbg=239
hi Search ctermfg=235 ctermbg=186 cterm=bold
hi IncSearch ctermfg=235 ctermbg=208 cterm=bold
hi MatchParen ctermfg=235 ctermbg=161 cterm=bold
" Popup menu
hi Pmenu ctermfg=188 ctermbg=237
hi PmenuSel ctermfg=235 ctermbg=118 cterm=bold
hi PmenuSbar ctermbg=239
hi PmenuThumb ctermfg=81
" General syntax
hi Identifier ctermfg=252 " Variables - Yellow
hi Function ctermfg=118 " Functions - Green
hi Statement ctermfg=135 cterm=bold " Keywords - Purple
hi Operator ctermfg=161 " Operators - Red
hi Keyword ctermfg=135 cterm=bold " Keywords - Purple
hi Constant ctermfg=135 " Constants - Purple
hi String ctermfg=221 " Strings - Orange
hi Number ctermfg=135 " Numbers - Purple
hi Boolean ctermfg=135 cterm=bold " Booleans - Purple
hi Type ctermfg=81 " Types - Blue
hi Special ctermfg=81 cterm=italic
hi Delimiter ctermfg=161
hi Error ctermfg=161 ctermbg=235 cterm=bold,underline
hi Todo ctermfg=235 ctermbg=220 cterm=bold
" Python specific
hi pythonStatement ctermfg=135 cterm=bold
hi pythonConditional ctermfg=161 cterm=bold
hi pythonRepeat ctermfg=161 cterm=bold
hi pythonOperator ctermfg=161 cterm=bold
hi pythonInclude ctermfg=161 cterm=bold
hi pythonFunction ctermfg=118 cterm=bold
hi pythonComment ctermfg=244 cterm=italic
hi pythonString ctermfg=221
hi pythonNumber ctermfg=135
hi pythonBuiltin ctermfg=81 cterm=bold
hi pythonSelf ctermfg=161 cterm=italic
" Define colors for our custom attribute highlighting
hi MyDot ctermfg=161 " Dots - Red
hi MyAttr ctermfg=208 " Attributes after dots - Orange
" Set up the working syntax highlighting for Python files
augroup PythonAttributeHighlight
autocmd!
autocmd FileType python syntax match MyDot /\./
autocmd FileType python syntax match MyAttr /[a-zA-Z_][a-zA-Z0-9_]*/ contained
autocmd FileType python syntax region MyDotted start=/\./ end=/\>/ contains=MyDot,MyAttr
" Function name highlighting
autocmd FileType python syntax match pythonFunctionName /\<\h\w*\ze\s*(/
autocmd FileType python hi pythonFunctionName ctermfg=118 cterm=bold
autocmd FileType python syntax match pythonClassName /\<class\s\+\zs\h\w*/
autocmd FileType python hi pythonClassName ctermfg=81 cterm=bold
" Custom flow control highlighting
autocmd FileType python syntax clear pythonStatement
autocmd FileType python syntax keyword pythonStatement def class with as import from global nonlocal lambda yield async await
autocmd FileType python syntax keyword pythonFlowControl return break continue raise pass if else elif
autocmd FileType python syntax keyword pythonRepeat for while do
autocmd Filetype python syntax keyword pythonBoolean True False None
autocmd FileType python hi pythonFlowControl ctermfg=161 cterm=bold
autocmd FileType python hi pythonRepeat ctermfg=161 cterm=bold
autocmd FileType python hi pythonBoolean ctermfg=135 cterm=bold
augroup END
Thanks!
syntax …commands toafter/syntax/python.vim, where they belong, and there is no reason for adding thosehi …commands in via autocommands. Keep things simple.