Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gvim to custom highlight group in .vimrc not working

Tags:

vim

unix

vim version 6.3

Trying to create a new highlight group to highlight tabs and trailing spaces. I only have these settings in .vimrc.

autocmd ColorScheme * highlight UnwanttedTab ctermbg=red guibg=darkred
match UnwanttedTab /\t/
autocmd ColorScheme * highlight TrailSpace guibg=red ctermbg=darkred
match TrailSpace / \+$/ 
colorscheme torte

But when launching vim, I got error like this:

line    1:
E216: No such group or event: ColorScheme * highlight UnwanttedTab ctermbg=red guibg=darkred
line    2:
E28: No such highlight group name: UnwanttedTab /\t/
line    3:
E216: No such group or event: ColorScheme * highlight TrailSpace guibg=red ctermbg=darkred
line    4:
E28: No such highlight group name: TrailSpace / \+$/

I was following this guide and using autocmd to prevent my highlight settings to be cleared. But still can't get to work. Does anyone know what could be wrong?

like image 680
Stan Avatar asked Jun 18 '12 16:06

Stan


1 Answers

The problem with your code is that the match commands reference custom highlight group names that aren't defined yet. Those group names must be defined using e.g. highlight UnwanttedTab ... before you use them with match. The autocmds will only define the highlight group names using highlight after you've loaded any colorscheme.

This is a way to setup the highlight groups, first defining syntax highlighting with group names, then providing a match for those group names:

highlight UnwanttedTab ctermbg=red guibg=darkred
highlight TrailSpace guibg=red ctermbg=darkred
match UnwanttedTab /\t/
match TrailSpace / \+$/ 

The autocmd recommendation from the article is intended to prevent any colorschemes that you might load from clearing your custom highlight groups with :highlight clear.

Try combining the commands above with the autocmd, in your .vimrc:

highlight UnwanttedTab ctermbg=red guibg=darkred
highlight TrailSpace guibg=red ctermbg=darkred
match UnwanttedTab /\t/
match TrailSpace / \+$/ 

autocmd ColorScheme * highlight UnwanttedTab ctermbg=red guibg=darkred
autocmd ColorScheme * highlight TrailSpace guibg=red ctermbg=darkred

colorscheme torte
like image 79
pb2q Avatar answered Sep 19 '22 16:09

pb2q