5

I try to make python.vim color similar to material theme (sublime text 3), see pictures: https://imgur.com/a/wOgH7

expected is material theme and current is mine.

You may noticed that all method, function for i.e: os.path.exists(replacement_file)

all os.path.exists and replacement_file are in green color but currently I can't do that with python.vim hacks.

Any help and suggestions is highly appreciated. thanks.

EDIT: to narrow down the situation, I have sample below:

basename = os.path.splitext(filename)[0]
 if(os.path.isfile(candidate) or os.path.isdir(candidate)):

Here I'm trying to match os.path.splitext and os.path.isfile and os.path.isdir that all the name of method or function.

What have I achieved so far?

I could match os.path.isfile with this regex (\()(\w+\.)*\w+\( but above regex will not match os.path.splitext and os.path.isdir that said have space before them. Any help?

4

A regexp that seems to work well is:

\v[[:alpha:]_.]+\ze(\s?\()

Or for syntax highlighting (see How can I add additional syntax highlighting rules in my local vimrc? on how to add this to your vimrc):

:syntax match pythonFunction /\v[[:alpha:]_.]+\ze(\s?\()/
:hi def link pythonFunction Function

To break it down:

  • \v – "very magic" mode so we don't have to add so many backslashes.
  • [[:alpha:]_.]+ – function name, any alphanumeric character, _, or . repeated 1 or more times.
  • \ze – Match the following group "zero-width", meaning it won't be included in the match. This matters because otherwise the space and ( will be highlighted as well.
  • ( – start subgroup, for the \ze.
  • \s? – Optionally match any space.
  • \( – match (.
  • ) – end subgroup.
  • thanks, it works. but if i try to match region inside () of method, say function(MATCH_HERE) by create a region like follow: syn match pFun "\v[[:alpha:]_.]+\ze(\s?()" "contained nextgroup=pFunVars syn region pFunVars start="(" end=")" contained contains=pFunPara transparent keepend syn match pFunPara "[^,]*" contained contains=pythonSelf,pythonOperatorExt,pythonExtraOperator,pythonBuiltin,pythonConstant,pythonStatement,pythonNumber,pythonString,pythonBrackets skipwhite the match failed. any suggestions? – Tuyen Pham Oct 3 '17 at 13:51
  • @TuyenPham Matching variables inside the function is not what you asked ;-) As you can see, comments don't support newlines and aren't very suited for discussing these sort of issues with code, so assuming that this answer satisfactory answered the asked question, it's probably best to just ask a new question about that if you have further problems with highlighting Python syntax files. – Martin Tournoij Oct 3 '17 at 14:00

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.