简体   繁体   English

如何让 Vim 突出显示非 ascii 字符?

[英]How to get Vim to highlight non-ascii characters?

I'm trying to get Vim to highlight non-ASCII characters.我试图让 Vim 突出显示非 ASCII 字符。 Is there an available setting, regex search pattern, or plugin to do so?是否有可用的设置、正则表达式搜索模式或插件来执行此操作?

Using range in a [] character class in your search, you ought to be able to exclude the ASCII hexadecimal character range, therefore highlighting (assuming you have hlsearch enabled) all other characters lying outside the ASCII range:在搜索中使用[]字符类中的范围,您应该能够排除ASCII 十六进制字符范围,因此突出显示(假设您启用了hlsearch )所有位于 ASCII 范围之外的其他字符:

/[^\x00-\x7F]

This will do a negative match (via [^] ) for characters between ASCII 0x00 and ASCII 0x7F (0-127), and appears to work in my simple test.这将对 ASCII 0x00和 ASCII 0x7F (0-127) 之间的字符进行否定匹配(通过[^] ),并且在我的简单测试中似乎有效。 For extended ASCII, of course, extend the range up to \\xFF instead of \\x7F using /[^\\x00-\\xFF] .对于扩展的ASCII,当然,向上延伸至\\xFF代替\\x7F使用/[^\\x00-\\xFF]

You may also express it in decimal via \\d :您也可以通过\\d以十进制表示:

/[^\d0-\d127]

If you need something more specific, like exclusion of non-printable characters, you will need to add those ranges into the character class [] .如果您需要更具体的内容,例如排除不可打印的字符,则需要将这些范围添加到字符类[]

Yes, there is a native feature to do highlighting for any matched strings.是的,有一个本地功能可以突出显示任何匹配的字符串。 Inside Vim, do:在 Vim 中,执行以下操作:

:help highlight
:help syn-match

syn-match defines a string that matches fall into a group. syn-match定义了一个匹配属于一个组的字符串。 highlight defines the color used by the group. highlight定义组使用的颜色。 Just think about syntax highlighting for your vimrc files.想想你的 vimrc 文件的语法高亮。

So you can use below commands in your .vimrc file:所以你可以在你的 .vimrc 文件中使用以下命令:

syntax match nonascii "[^\x00-\x7F]"
highlight nonascii guibg=Red ctermbg=2

For other (from now on less unlucky) folks ending up here via a search engine and can't accomplish highlighting of non-ASCII characters, try this (put this into your .vimrc):对于其他(从现在开始不太走运)通过搜索引擎来到这里并且无法突出显示非 ASCII 字符的人,试试这个(把它放到你的 .vimrc 中):

highlight nonascii guibg=Red ctermbg=1 term=standout
au BufReadPost * syntax match nonascii "[^\u0000-\u007F]"

This has the added benefit of not colliding with regular (filetype [file extension] based) syntax definitions.这具有不与常规(基于文件类型 [文件扩展名])语法定义冲突的额外好处。

This regex works as well.这个正则表达式也有效。 It was the first google hit for "vim remove non-ascii characters" from briceolion.com and with :set hlsearch will highlight:这是来自briceolion.com 的“vim remove non-ascii characters”的第一个谷歌点击,并且使用:set hlsearch将突出显示:

/[^[:alnum:][:punct:][:space:]]/

If you are interested also in the non printable characters use this one: /[^\\x00-\\xff]/如果您也对不可打印的字符感兴趣,请使用以下字符: /[^\\x00-\\xff]/

I use it in a function:我在一个函数中使用它:

 function! NonPrintable()
   setlocal enc=utf8
   if search('[^\x00-\xff]') != 0
     call matchadd('Error', '[^\x00-\xff]')
     echo 'Non printable characters in text'
   else
     setlocal enc=latin1
     echo 'All characters are printable'
   endif
 endfunction

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 <Cw>1 .基于有关此主题的其他答案和我在这里得到的答案我已将其添加到我的.vimrc ,以便我可以通过键入<Cw>1来控制非 ascii 突出显示。 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也就是说,如果您要编辑一个 zsh 文件,则需要将zshComment添加到该行

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).否则它不会显示非 ASCII 字符(如果您想确保在所有组中显示非 ASCII 字符,您也可以设置 containsin=ALL)。 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.要检查如何在不同的文件类型上调用注释,请打开所需类型的文件并在 vim 上输入:sy ,然后搜索注释的语法项。

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 <C-w>1 :call ToggleHighlightNonascii()<CR>

Somehow none of the above answers worked for me.不知何故,上述答案都不适合我。

So I used :1,$ s/[^0-9a-zA-Z,-_\\.]//g所以我用:1,$ s/[^0-9a-zA-Z,-_\\.]//g

It keeps most of the characters I am interested in.它保留了我感兴趣的大部分角色。

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 syntax group in the matter).但是,对于仍然有问题的其他人,这里是另一种突出显示注释中的非 ascii 字符(或任何语法组)的解决方案。 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

This has mix parts from other solutions.这有来自其他解决方案的混合部分。 You may remove contained , but, from documentation, there may be potential problem of recursing itself (as I understand).您可以删除contained ,但是,从文档中,可能存在递归本身的潜在问题(据我所知)。 To view other defined patterns, syn-contains section would contain it.要查看其他定义的模式, syn-contains部分将包含它。

:help syn-containedin
:help syn-contains 

Replicated issue from: Set item to higher highlight priority on vim复制的问题来自: 在 vim 上将项目设置为更高的突出显示优先级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM