I\'m trying to get Vim to highlight non-ASCII characters. Is there an available setting, regex search pattern, or plugin to do so?
Based on the other answers on this topic and the answer I got here I've added this to my .vimrc, so that I can control the non-ascii highlighting by typing . It also shows inside comments, although you will need to add the comment group for each file syntax you will use. That is, if you will edit a zsh file, you will need to add zshComment to the line
au BufReadPost * syntax match nonascii "[^\x00-\x7F]" containedin=cComment,vimLineComment,pythonComment
otherwise it won't show the non-ascii character (you can also set containedin=ALL if you want to be sure to show non-ascii characters in all groups). To check how the comment is called on a different file type, open a file of the desired type and enter :sy on vim, then search on the syntax items for the comment.
function HighlightNonAsciiOff()
echom "Setting non-ascii highlight off"
syn clear nonascii
let g:is_non_ascii_on=0
augroup HighlightUnicode
autocmd!
augroup end
endfunction
function HighlightNonAsciiOn()
echom "Setting non-ascii highlight on"
augroup HighlightUnicode
autocmd!
autocmd ColorScheme *
\ syntax match nonascii "[^\x00-\x7F]" containedin=cComment,vimLineComment,pythonComment |
\ highlight nonascii cterm=underline ctermfg=red ctermbg=none term=underline
augroup end
silent doautocmd HighlightUnicode ColorScheme
let g:is_non_ascii_on=1
endfunction
function ToggleHighlightNonascii()
if g:is_non_ascii_on == 1
call HighlightNonAsciiOff()
else
call HighlightNonAsciiOn()
endif
endfunction
silent! call HighlightNonAsciiOn()
nnoremap 1 :call ToggleHighlightNonascii()