How do I create Vim syntax highlighting rules that are common to all filetypes?

▼魔方 西西 提交于 2019-12-11 06:56:18

问题


I'm trying to highlight trailing whitespace in vim, regardless of filetype.

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

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

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

in just one place in my .vim directory structure and have them apply to all filetypes.

I've tried adding them to .vimrc and I've tried adding them to ~/.vim/plugin/settings.vim and that didn't work either.

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.


回答1:


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.

And here is what it looks like:



来源:https://stackoverflow.com/questions/21948232/how-do-i-create-vim-syntax-highlighting-rules-that-are-common-to-all-filetypes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!