Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set item to higher highlight priority on vim

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?

like image 494
Werner Avatar asked Dec 29 '14 01:12

Werner


People also ask

How do I enable highlighting in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does Vim support syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.


Video Answer


2 Answers

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
like image 81
Ingo Karkat Avatar answered Sep 22 '22 18:09

Ingo Karkat


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 
like image 20
nate Avatar answered Sep 18 '22 18:09

nate