简体   繁体   English

VIM:放入.vimrc时,match none命令不起作用

[英]VIM: match none command not working when put in .vimrc

I have the following in my .vimrc 我的.vimrc中有以下内容

syn match ErrorLeadSpace /^ \+/         " highlight any leading spaces
syn match ErrorTailSpace / \+$/         " highlight any trailing spaces
syn match Error80        /\%>80v.\+/    " highlight anything past 80 in red

au FileType c match error /\s\+$\|\%>80v.\+\|[ ][ ]\+\|\n\n\n\+\|,[^ ]\|^[ ]\+[^\*]\|(\s\+\|\s\+)/
au FileType c highlight error ctermbg=red guibg=red ctermfg=blue guifg=blue
au FileType h match error /\s\+$\|\%>80v.\+\|[ ][ ]\+\|\n\n\n\+\|,[^ ]\|^[ ]\+[^\*]\|(\s\+\|\s\+)/
au FileType h highlight error ctermbg=red guibg=red ctermfg=blue guifg=blue

As a result, vim highlights leading/trailing whitespaces or the excess of characters in a line exceeding 80 chars. 结果,vim突出显示行距/尾随空格或超过80个字符的行中多余的字符。

However, for the time being, I want to disable this 'error' highlighting. 但是,暂时,我想禁用此“错误”突出显示。 Currently, I'm achieving this by using the command "match none", in the opened file. 目前,我是通过在打开的文件中使用“不匹配”命令来实现的。 Whereas, this is not working when I'm putting this command in the .vimrc file. 而当我将此命令放入.vimrc文件时,此方法不起作用。

How can I achieve this with minimal changes to my .vimrc? 如何以最小的.vimrc更改实现此目标?

The match none command doesn't work in the .vimrc file, because you put your highlight rules into an au command (which is quite a good idea). match none命令在.vimrc文件中不起作用,因为您将突出显示规则放入了au命令中(这是个好主意)。 The au will execute each time you will edit a new C file, and the match none command from your .vimrc will be useless, because it was sourced already a long time ago, on load. 每次您编辑一个新的C文件时, au都会执行,而.vimrc中的match none命令将毫无用处,因为它是很早以前就已加载的。

There are several problems inside the code you gave; 您提供的代码内部有几个问题; I explain it below. 我在下面解释。 But you can do it like this for example: 但是您可以这样做,例如:

highlight CError ctermbg=red guibg=red ctermfg=blue guifg=blue

function! DefineAugroup_For_C()
    augroup MyCAugroup
        au!
        au FileType c match CError /\s\+$\|\%>80v.\+\|[ ][ ]\+\|\n\n\n\+\|,[^ ]\|^[ ]\+[^\*]\|(\s\+\|\s\+)/
    augroup END
endf

" Enable the automatic highlight for future edited files, and also for the current one:
command! SetHighlightForC call DefineAugroup_For_C()|exe "set ft=".&ft

" Disable the automatic highlight for future edited files, and also for the current one:
command! UnsetHighlightForC augroup! MyCAugroup|match none

" Comment this line to unable the automatic highlight on load:
SetHighlightForC

And then you can lively desactivate/activate the highlighting like this: 然后您可以像这样生动地停用/激活突出显示:

:UnsetHighlightForC
:SetHighlightForC

I think there are some problems in your code: 我认为您的代码中存在一些问题:

  • The three first lines don't refer to any existing highlight ( ErrorLeadSpace , ErrorTailSpace and Error80 ), so unless you defined them in another place with the highlight command, it's useless. 三个第一行不涉及任何现有的亮点( ErrorLeadSpaceErrorTailSpaceError80 ),所以,除非你与另一个地方定义他们highlight命令,也没用。 (At least, it's useless for your question). (至少,对您的问题没有用)。

  • Another problem is that you don't need to add these lines: 另一个问题是您不需要添加以下行:

     au FileType h match error /\\s\\+$\\|\\%>80v.\\+\\|[ ][ ]\\+\\|\\n\\n\\n\\+\\|,[^ ]\\|^[ ]\\+[^\\*]\\|(\\s\\+\\|\\s\\+)/ au FileType h highlight error ctermbg=red guibg=red ctermfg=blue guifg=blue 

    since the C header files don't have the h filetype, but the c filetype, as well as the source files. 因为C头文件没有h文件类型,但是c文件类型以及源文件。 The h filetype doesn't exist by default. 默认情况下, h文件类型不存在。 To know the filetype of a file, do :set ft? 要知道文件的文件类型, :set ft?

  • Another thing : if you want to add the same rule for several filetypes, you can add them in only one command, by separating the filetypes with a comma, like this: 另一件事:如果要为多个文件类型添加相同的规则,则可以通过用逗号分隔文件类型,从而仅用一个命令添加它们,如下所示:

     au FileType c,cpp,php match error /\\s\\+$\\|\\%>80v.\\+\\|[ ][ ]\\+\\|\\n\\n\\n\\+\\|,[^ ]\\|^[ ]\\+[^\\*]\\|(\\s\\+\\|\\s\\+)/ 

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

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