Spike Lee
Spike Lee

Reputation: 419

How to add custom python 'self' syntax highlighting in vim

I know how to turn on the custom syntax highlighing when editing python files

like:

:syn keyword Keyword self

However, It is annoying because I need to put the code every time.

I want to just edit .vimrc file.

How to do it?

Upvotes: 2

Views: 2163

Answers (4)

Ingo Karkat
Ingo Karkat

Reputation: 172738

While @AndrewRadev's solution works, there's actually a built-in extension mechanism for syntax scripts via the :help after-directory.

Just put the syntax extensions into ~/.vim/after/syntax/python.vim; this will be automatically sourced after the built-in $VIMRUNTIME/syntax/python.vim, and extend it:

syn keyword pythonStatement self

This is much simpler than using a filetype plugin to define a hook into the Syntax event.

Upvotes: 2

ap0
ap0

Reputation: 1173

My solutions is not the best because I change Vim internal files but it works for me and was the quickest solution. I simply added the keyword self into the file $VIMRUNTIME/syntax/python.vim which looks like:

syn keyword pythonStatement self

Upvotes: 1

anon
anon

Reputation:

For a command that you run every time when you open a particular filetype, you can use autocommands (:help autocmd-intro).

In this particular case, you want to run some extra syntax code, so it makes sense to listen for the Syntax event of the python filetype (:help Syntax ):

augroup python_syntax_extra
  autocmd!
  autocmd! Syntax python :syn keyword Keyword self
augroup END

Putting this in your .vimrc should do the trick. The augroup stuff is only to ensure the autocommands in this group ("python_syntax_extra") don't get run twice (:help :augroup).

Alternatively, you can put this in ~/.vim/ftplugin/python.vim to organize common python settings. If you do that, you probably don't need the autocmd dance, even:

autocmd! Syntax <buffer> :syn keyword Keyword self

Upvotes: 1

Kir Chou
Kir Chou

Reputation: 3080

You can achieve this simply by putting this line into .vimrc

match Keyword /self/

Upvotes: 2

Related Questions