简体   繁体   中英

Issues understanding Vim's regex engine

I am trying to set up a script for the conceal feature to prettify strings in Python. This worked fine so far for single word substitutions. But now I would like to replace "not in" with "∉". I tried this:

syntax match pyOperator "not in" conceal cchar=∉

But that does not match anything and I don't see why not. eg

x not in l stays x not in l

However

syntax match pyOperator " not in " conceal cchar=∉

works. But I want the former version, as this one makes

x not in l to x∉l , hiding the spaces.

Why does the second version work and the first does not and how can I make it work?

btw. I also tried other variants, such as

syntax match pyOperator "\s\+not\s\+in\s\+'" conceal cchar=∉

That one does not work t all either, which also puzzles me, as t is a superset of the second version.

It is being blocked by the syntax keyword pythonOperator and in is or not definition, which seems to take precedence over syntax match (and doesn't allow for overlap). So we'll clear out that definition, and replace it with an equivalent syntax match one.

" ~/.vim/after/syntax/python.vim
syn clear pythonOperator
syn match pythonOperator /and\|is\|or\|not/
syn match pythonOperator /not in/ conceal cchar=∉
syn match pythonOperator /in/ conceal cchar=∈

I also changed your pyOperator to the standard pythonOperator ; one reason is that it's already there (for in , and , is , or and not , as seen above); the other reason (from :help 44.2 ):

By convention, each group name is prefixed by the filetype for the language being defined. [...] In a syntax file for "csh" scripts the name "cshType" would be used. Thus the prefix is equal to the value of 'filetype'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM