Highlight non ascii characters

75 views
Skip to first unread message

Peter Nguyen

unread,
Jan 24, 2014, 11:13:48 PM1/24/14
to vim...@googlegroups.com
Hi,

I have a twig (PHP HTML template) file with several non ascii characters at different places. I can do a search with:

/[\x7f-\xff]/

and it will match them. But when I try to add these commands to my .vimrc file for highlighting them, it does not work:

" Find non ASCII chars and highlight them
syn match nonascii "[\x7f-\xff]"
hi nonascii guibg=Red ctermbg=2

Anyone who knows what I'm doing wrong?

Niklas Lindström

unread,
Jan 25, 2014, 4:37:22 PM1/25/14
to vim...@googlegroups.com
Hi Peter,

Declaring syntax rules in the .vimrc will not work, since the syntax will be cleared during vim startup (or something to that effect).

For your case, you can either add something like this to your .vimrc:

    match ErrorMsg "[\x7f-\xff]"

Or add your original commands to a file called ~/.vim/after/syntax/php.vim in your home directory (create it and any needed parent directories if they don't already exist). The "after" folder is a nice way to customize things locally according to your needs. That file specifically will extend the php syntax for you.

See also e.g. <http://vim.wikia.com/wiki/Highlight_unwanted_spaces>.

H.T.H.

Cheers,
Niklas

 

--
--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_mac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_mac+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter Nguyen

unread,
Jan 25, 2014, 5:11:01 PM1/25/14
to vim...@googlegroups.com
Hi Niklas,

thanks for your answer. The match ErrorMsg "[\x7f-\xff]" works but only on the current file (.vimrc) after I source it. If I check the other file (twig) it didn't highlight the non-ascii characters. I haven't tried your suggestion with the "after" folder yet because I would want that non-ascii characters to be highlighted in all files, not just for php syntax. Is it possible?

Niklas Lindström

unread,
Jan 26, 2014, 3:05:46 AM1/26/14
to vim...@googlegroups.com
Hello,

Oh, right. It seems you need to trigger this for each window, so you can do either:

    au BufWinEnter * match ErrorMsg "[\x7f-\xff]"

Or perhaps better (to play nicer with other things adding matches):

    au BufWinEnter * let w:matchnonascii=matchadd('ErrorMsg', "[\x7f-\xff]", -1)

See e.g. <http://vim.wikia.com/wiki/VimTip810> for details. (Or :help :match / :help matchadd())

Peter Nguyen

unread,
Jan 27, 2014, 6:07:21 AM1/27/14
to vim...@googlegroups.com
Thanks for your help! It works as expected now. Now I just need to figure out how to highlight invisble characters that are not ascii and skip the other visible unicode characters (such as the Swedish åäö) as it is. That's another thread though.
Reply all
Reply to author
Forward
0 new messages