如何创建所有文件类型通用的Vim语法突出显示规则? [英] How do I create Vim syntax highlighting rules that are common to all filetypes?
问题描述
无论文件类型如何,我都试图突出显示vim中的尾随空白.
I'm trying to highlight trailing whitespace in vim, regardless of filetype.
我能够按文件类型进行操作.例如,对于HTML:
I'm able to do it on a per-filetype basis. For example, for HTML:
〜/.vim/ftplugin/html.vim
color html
〜/.vim/syntax/html.vim
syn match TrailingSpace display excludenl /\s\+$/
〜/.vim/colors/html.vim
hi TrailingSpace ctermbg=52
〜/.vimrc
syntax on
filetype on
filetype plugin on
使用这些设置可以使行末尾的空格以红色背景突出显示. (它们还覆盖了vim的所有内置HTML语法和突出显示功能,但我对此表示满意.)
These settings make it so that trailing spaces at the end of a line are highlighted with a red background. (They also override all of vim's built-in HTML syntax and highlighting, but I'm fine with that.)
麻烦的是,例如,如果要对JavaScript执行相同的操作,则必须在该文件中添加相同的syn
和hi
行,对于其他所有文件类型,依次类推.
The trouble is that if I want to do the same for JavaScript, for instance, I have to add the same syn
and hi
line to that file, and so on for every other filetype.
我想做的是把这两个行放在 :
What I would like to be able to do is put these two lines:
syn match TrailingSpace display excludenl /\s\+$/
hi TrailingSpace ctermbg=52
在我的.vim
目录结构中的一个位置,并将它们应用于所有文件类型.
in just one place in my .vim
directory structure and have them apply to all filetypes.
我尝试将它们添加到.vimrc
中,并且尝试将它们添加到~/.vim/plugin/settings.vim
中,但这也不起作用.
I've tried adding them to .vimrc
and I've tried adding them to ~/.vim/plugin/settings.vim
and that didn't work either.
其他hi
行普遍适用,但仅适用于内置vim语法项目,例如MatchParen
和IncSearch
.
Other hi
lines I've added to ~/.vim/plugin/settings.vim
worked universally, but only for built-in vim syntax items, e.g. MatchParen
and IncSearch
.
有没有可以在上面添加两行的文件,它可以工作吗?我可以将它们拆分并放在单独的文件中,但是理想情况下,我无需在其他任何位置添加任何内容.
Is there any file where I could add just the two lines above and it would work? I can split them up and put them in separate files, but ideally, I wouldn't have to add anything anywhere else.
推荐答案
以下是我自己对完全相同的问题的解决方案:
Here is my own solution to the exact same issue:
autocmd BufEnter,WinEnter * call matchadd("Error", "\\s\\+$", -1)
我从相同的语法"方法开始,但最终遇到了您现在面临的相同问题,没有简单的解决方案.
I started with the same "syntax" method but I ended up facing the same issues that you are facing right now with no simple solution.
这是它的样子:
这篇关于如何创建所有文件类型通用的Vim语法突出显示规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!