6

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!

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
xlc
  • 150
  • 8
  • 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! – xlc Dec 13 '16 at 02:45

1 Answers1

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.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
  • 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? – xlc 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? – xlc Dec 20 '16 at 01: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 01:53
  • I add python.vim to a colorscheme after directory, i.e., my_colorscheme/after/syntax/python.vim , not ~/.vim/after/syntax/python.vim. Is it possible to be caused by this? It just raised error, the highlighting already works. – xlc Dec 20 '16 at 02:02
  • @Jimmy Yes, could be that's causing the error. Why add it there and not just ~/.vim/after/syntax/python.vim? – Martin Tournoij Dec 20 '16 at 02:05
  • It seems not be caused by the different postion of python.vim. ``` – xlc Dec 20 '16 at 02:09
  • Error detected while processing /Users/xlc/.vim/after/syntax/python.vim: line 15: E28: No such highlight group name: pythonAttribute. The problem persists. – xlc Dec 20 '16 at 02:10
  • @Jimmy Which version of Vim are you using? – Martin Tournoij Dec 20 '16 at 02:16
  • vim8.0, included patches:1-94, I installed it with brew. – xlc Dec 20 '16 at 02:18
  • @Jimmy Dunno then :-/ All I can say is that the script works for me as-is (just tested again to be sure). Are you sure you're using the standard Python syntax highlighting and not some plugin (:verbose hi pythonString could tell you which one you're using)? Or perhaps there's something else interfering with this for whatever reason? You can try following these steps to isolate the problem. – Martin Tournoij Dec 20 '16 at 02:25
  • @Cartpetsmoker It is the python-mode plugin! I disable it and the problem gone. Thanks! – xlc Dec 20 '16 at 02:29
  • @Jimmy Ah, yes. The syntax file for that plugin is completely different from the one that Vim ships with. Actually, it looks like that syntax file already sort-of does what I did in my answer. Just using :hi pythonClass ctermfg=darkgreen guifg=darkgreen should already work for that. – Martin Tournoij Dec 20 '16 at 02:32