7

How can I define a syntax to highlight the python class name MyClass in vim?

class MyClass(object):
    ......

Vim has already highlighted the class name. However, it seemingly highlights it with the function name in a same way. Can I have a better way to highlight the two categories?

Is it a right way via syn match ...? In the default syntax/python.vim, I found this:

syn match   pythonFunction  "\h\w*" display contained

I have tried, but my syn definition must be wrong, it does not work.

syn match pythonClassName "^class \(*\):" display contained
call HI('pythonClassName',         124, '', 'bold', 'bold')

HI() is a self-defined function, I have used it to modify pythonStatement successfully.

As a matter of fact, I do not know what it really does. It's just a try. Could someone give me some tips? Thanks!

  • 1
    Welcome on our site! What are you trying to do exactly? Vim should already highlight the class (at least it does on my system). If it doesn't, maybe you should try to use syntax on in your vimrc. If it is highlighted but you want to change the highlighting, see :h mysyntaxfile-add to know how to override the current syntax highlighting file. In any case you should be more specific with what you already tried (which command, in which file, etc). – statox Dec 12 '16 at 14:37
  • Also note that wikia has a useful tip to show the syntax group used under the cursor. – statox Dec 12 '16 at 14:44
  • @statox Thank your for your comment! Vim truly has already highlight the class name. However, it seemingly highlights the class name and the function name in a same way. Could I have a better way to distinguish the class name and function name? By the way, I have modified the question to be more specific, thanks! – Jimmy Dec 13 '16 at 2:45
2

However, it seemingly highlights it with the function name in a same way.

With :echo synIDattr(synID(line('.'), col('.'), 1), 'name') we can get the name of the syntax group, which is indeed pythonFunction.

Looking at /usr/share/vim/vim80/syntax/python.vim we can see this is highlighted with:

syn keyword pythonStatement     class def nextgroup=pythonFunction skipwhite
syn match   pythonFunction  "\h\w*" display contained
hi def link pythonFunction      Function

There is no facility to highlight classes separately. If you want to add this, you can use something like:

" Clear default
syn clear pythonStatement

" Set it to what's in the Python file, minus the class.
syn keyword pythonStatement    False None True
syn keyword pythonStatement    as assert break continue del exec global
syn keyword pythonStatement    lambda nonlocal pass print return with yield
syn keyword pythonStatement    def nextgroup=pythonFunction skipwhite

" Now make seperate syntax groups for the class.
syn keyword pythonClassStmt class nextgroup=pythonClass skipwhite
syn match   pythonClass "\h\w*" display contained

" Avoid highlighting attributes as builtins – just added "pythonClass" here.
syn clear pythonAttribute
syn match   pythonAttribute /\.\h\w*/hs=s+1
    \ contains=ALLBUT,pythonBuiltin,pythonFunction,pythonClass,pythonAsync
    \ transparent

" Highlight the class statement and the class name.
hi def link pythonClassStmt Statement
hi pythonClass ctermfg=darkgreen guifg=darkgreen

I used darkgreen because this is what Ruby uses, but feel free to use any colour you like. You could highlight the class keyword any way you'd like.

To make this loads after the built-in Python syntax file add this to ~/.vim/after/syntax/python.vim.

Also see: Fixing “missing” syntax highlighting for python for a general primer on syntax highlighting and some more details.

  • Thank you for your answer! However, I tried your solution only to find not only the python class name was highlighted but some other things were changed simultaneously, e.g., the X_train in self.X_train was highlighted in a same way, which is not expected. Could it be possible to only change the highlighting of the class name? – Jimmy Dec 17 '16 at 12:25
  • @Jimmy Oops, looks like I didn't test this enough. Thanks for pointing that out. I think I know what's wrong, but don't have time to update my answer right now. I'll update it later. – Martin Tournoij Dec 17 '16 at 15:18
  • @Jimmy Answer updated. The problem was that I had forgot to overwrite the pythonAttribute syntax group. – Martin Tournoij Dec 19 '16 at 23:38
  • Many thanks! It works. But there is still a minor error raised when python file is opened, E28: No such highlight group name: pythonAttribute. Could you test it again? – Jimmy Dec 20 '16 at 1:50
  • @Jimmy I don't have that error. I'm also confused, since that error comes from the highlight command rather than the syntax command. Are you sure this code is giving that error? Or is it some other script? – Martin Tournoij Dec 20 '16 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.