Set item to higher highlight priority on vim

a 夏天 提交于 2019-12-07 02:27:14

问题


I want to non ascii characters to show as discussed here, but the syntax highlight disappears when the non ascii character are inside a comment. Investigating a little the problem, I've discovered at the vim-manual that an item that starts earlier has higher priority (3rd item). From help :syn-priority:

When several syntax items may match, these rules are used:

  1. When multiple Match or Region items start in the same position, the item defined last has priority.

  2. A Keyword has priority over Match and Region items.

  3. An item that starts in an earlier position has priority over items that start in later positions.

I am currently using this:

syntax match nonascii "[^\x00-\x7F]" 
highlight nonascii cterm=underline ctermfg=red ctermbg=none term=underline

I tried to give higher priority to nonascii match item using the options nextgroup:

syntax match nonascii "[^\x00-\x7F]" nextgroup=Comment

and contains options:

syntax match nonascii "[^\x00-\x7F]" contains=ALL

but it didn't work. I also tried to disable comments temporarily (highlight clear Comment) without the desired effect (my comments got without highlight, but the nonascii continued unhighlighted). What I am missing?


回答1:


Yes, your custom syntax group isn't matched because there's already a match for comments (or other syntax elements from the existing syntax script).

The solution is to tell Vim that your nonascii group is containedin those groups, so that Vim will attempt to match there (and not just at the uncolored top level), too. What's complicating this is that the syntax group for comments depends on the syntax script and therefore on the filetype (those the naming is quite regular). In the following example, I've used the names for C and Vimscript files:

:syntax match nonascii "[^\x00-\x7F]" containedin=cComment,vimLineComment



回答2:


Someone already have answered the question. However, for others that are still having problems, here is another solution to highlight non-ascii characters in comments (or any group in the matter). It's not the best, but it's a temporary fix.

One may try:

:syntax match nonascii "[^\u0000-\u007F]" containedin=ALL contained |
            \ highlight nonascii ctermfg=yellow guifg=yellow

It's very close to the original implementation and other solution. You may even remove contained, but, from documentation, there may be potential problem of recursing itself (as I understand). To view other defined patterns, syn-contains section would contain it.

:help syn-containedin
:help syn-contains 


来源:https://stackoverflow.com/questions/27682324/set-item-to-higher-highlight-priority-on-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!