Andy Stewart
Andy Stewart

Reputation: 5498

Why don't the syntax-match plus highlight commands work?

I want to get Vim (MacVim for now) to highlight non-ASCII characters. As per this answer I added these two lines to my vimrc:

syntax match nonascii "[^\x00-\x7F]"
highlight nonascii guifg=#ffffff guibg=#ff0000
" highlight link nonascii ErrorMsg     (this didn't work either)

And I pasted some text containing a right single quote (hex 2019) into an HTML file. Vim didn't highlight it.

But when I replaced the two lines above with the following, it worked:

syn match ErrorMsg /[^\x00-\x7F]/

Why didn't the first version work?

EDIT: further investigation shows neither version works when I open vim with my file. But both work if I execute them by hand when vim is already open.

Upvotes: 1

Views: 801

Answers (2)

Andy Stewart
Andy Stewart

Reputation: 5498

I was able to get my second version working by prepending autocmd BufEnter *:

autocmd BufEnter * syn match ErrorMsg /[\x00-\x7F]/

The idea came from this answer about non-filetype-specific syntax highlighting.

Upvotes: 1

Martin Tournoij
Martin Tournoij

Reputation: 27852

Your solution only works for the GUI (gvim); if you're using Vim from a terminal, add ctermbg and/or ctermfb, for example:

highlight nonascii guifg=#ffffff guibg=#ff0000 ctermbg=red

Upvotes: 1

Related Questions