How to get Vim to highlight non-ascii characters?

前端 未结 8 1190
萌比男神i
萌比男神i 2020-11-29 15:14

I\'m trying to get Vim to highlight non-ASCII characters. Is there an available setting, regex search pattern, or plugin to do so?

8条回答
  •  旧时难觅i
    2020-11-29 15:55

    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 1. 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()
    

提交回复
热议问题