Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Editing the python.vim syntax file to highlight like Textmate

Tags:

regex

vim

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/

like image 968
pfdevilliers Avatar asked Nov 29 '11 14:11

pfdevilliers


People also ask

How do I enable Python syntax highlighting in vim?

The command to enable syntax highlighting in vim is :syntax on , if you want it to be active everytime you launch vim, just add a line containing syntax on in your . vimrc file. maybe your vim doesn't have filetype detection enabled, try adding filetype on to your .

How does syntax highlighting work in vim?

Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations.

How do I enable syntax highlighting in vim Mac?

If you want to toggle this on/off (without creating a . vimrc file) simply type :syntax on while in vi/vim. Save this answer.


1 Answers

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.

like image 54
Spencer Rathbun Avatar answered Sep 28 '22 14:09

Spencer Rathbun