简体   繁体   中英

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

I have the following in my .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.

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.

How can I achieve this with minimal changes to my .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). 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.

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. (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. The h filetype doesn't exist by default. To know the filetype of a file, do :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\\+)/ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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