3

Python3 has the matrix multiplication operator (@) which is used to multiply numpy matrices. But when we write something like A = B @ C in vim, the @ C portion gets highlighted. Apparently Python has these things called decorators which also use @. Vim's syntax highlighting seems to confuse the operator for the decorator. We basically want the following:

A = B@C     # <-- Operator, this should look just like B+C

A = B \
@C          # <-- Still operator, '\' makes it multi-line

@D          # <-- Decorator, should be highlighted

The culprit seems to be this line from the python.vim file (I used the latest version from here):

syn match   pythonDecorator "@" display nextgroup=pythonDottedName skipwhite

It doesn't check if the @ is being used as an operator.

So I have two questions.

  1. How to get vim to highlight correctly?
  2. How to do this without mucking around with /usr/share/vim/vim74/syntax/python.vim (or whatever system file is being used)?

I don't think I will be using decorators, so one option is to just comment out the offending line above. But this requires us to manipulate system files.

  • 4
    No need to comment it out in the system files. If you’re certain you never want decorators highlighted you can instead add syntax clear pythonDecorator to .vim/after/syntax/python.vim to remove the syntax item. – Rich May 9 at 16:21
  • 2
    Note that this was fixed in Vim 8.0.0055 in 2016; you may want to consider using a newer Vim version (that last regexp scares me btw). You can also copy the newer syntax file to ~/.vim/syntax/python.vim to override the default one, which should probably work (unless it uses newer Vim features, I didn't check/test). – Martin Tournoij May 9 at 16:31
  • @MartinTournoij That's an answer if I know one... Would you mind turning it into an actual answer? Thanks! (Also, yeah, scary regex! I'm glad it was someone else's job to figure that one out.) 🤣 – filbranden May 10 at 6:59

Your Answer

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

Browse other questions tagged or ask your own question.