10
votes

I'm trying to edit the python.vim syntax file to duplicate the syntax highlighting for python in Textmate. The attached image illustrates the highlighting of function parameters which i'm struggling to achieve.

imgur pic of code

The self, a, b is highlighted in Textmate but not in Vim. I figured that I have to do the following.

  1. Match a new region

    syn region pythonFunction start="(" end=")" contains=pythonParameters skipwhite transparent
    
  2. Try to match a string followed by a comma

    syn match pythonParameters ".*" contained
    

So in point 2 the ".*" will match any string at the moment and must be expanded further to be correct. However i'm not sure if i'm on the right path since the match in 2 is not constrained to region between the brackets (). Any tips or input will be appreciated.

EDIT 1: If anyone wondered how it turned out eventually.

Here is my vim syntax highlighting for python.

imgur pic of code

EDIT 2: So just for ultimate thoroughness I created a github page for it.

http://pfdevilliers.github.com/Pretty-Vim-Python/

2
That's a little more complex than what I posted. I'd be interested in seeing your final code.Spencer Rathbun
Sure i'll share it on github as soon as its not as hackety hack as it is at the moment. I borrowed from the ruby.vim syntax and your example just gave me enough to figure out the rest I needed.pfdevilliers
github.com/pfdevilliers/Pretty-Vim-Python a link to the files on Github. Any improvements are welcome.pfdevilliers

2 Answers

6
votes

Ok, you've got a couple problems.

  1. There is already a region called pythonFunction, for highlighting def and function names.
  2. This region will match any parenthesis, anywhere

So, find the pythonFunction match, and change it to this:

syn match   pythonFunction
      \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained nextgroup=pythonVars

Adding nextgroup tells vim to match pythonVars after a function definition.

Then add:

syn region pythonVars start="(" end=")" contained contains=pythonParameters transparent keepend
syn match pythonParameters "[^,]*" contained skipwhite

Finally, to actually highlight it, find the HiLink section, and add:

HiLink pythonParameters       Comment

Change Comment to the grouping you want, or add your own. I'm using Statement myself.

0
votes

Vim, Highlight matching Parenthesis ( ), square brackets [ ], and curly braces: { }

The config options for setting the colors of the foreground and background under the cursor when over a parentheses, square bracket or curly brace is this one:

hi MatchParen      ctermfg=16  ctermbg=208 cterm=bold

To enable/disable the background color of the line under the cursor:

:set cursorline
:set nocursorline

To set the color of the background color of the line under the cursor:

hi VisualNOS                   ctermbg=999
hi Visual                      ctermbg=999

enter image description here

Here is my adaptation:

https://github.com/sentientmachine/erics_vim_syntax_and_color_highlighting