问题
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