Custom vim highlighting
I am editing some xml right now and I have been leaving myself some comments of things to come back to. Like this
<!-- Question: bla bla -->
I am editing with vim right now and I would like vim to highlight Question right now so that I can easily look through my code and find all the places I need to look at. I know I need to add something in my vimrc but I think that I might be searching for the wrong thing.
Update
I tried putting this in my .vimrc
but it had no effect:
syn keyword JakeAnnotation Question
hi JakeAnnotation gui=bold term=bold cterm=bold
Update 2
Actually I can see that what I did before had some effect because when I do this:
:hi
It shows me all of the things that it is highlighting and one of the entries is:
JakeAnnotation xxx term=bold cterm=bold ctermbg=6
(and the xxx is formatted correctly)
This leads me to beleave that I am just not defining Question properly. Does Question need to be on a line by itself?
Update 3
Ok so to user 22303's post I have this working:
highlight MyQuestion cterm=bold term=bold ctermbg=blue ctermfg=black
match MyQuestion /Question/
However I suspect that you are only allowed to have one match per file. Because when I do this:
highlight MyQuestion cterm=bold term=bold ctermbg=blue ctermfg=black
match MyQuestion /Question/
highlight MyRelook cterm=bold term=bold ctermbg=blue ctermfg=black
match MyRelook /Another look/
The first one stops working. (But the second one works).
Solution 1:
I think we've been feeding you some answers that aren't quite right. Here's something to try that I tested on my machine.
First, create your own new highlight group:
:highlight MyQuestions guifg=red guibg=green
Now, specify that the highlight group will exist whenever a pattern is matched:
:syntax match MyQuestions /Question/
That should start showing highlight of the text 'Question' on each line that has that text. To expand to show whole line you would change search text to have wildcards matching entire line, something like this:
:syntax match MyQuestions /.*Question.*/
Solution 2:
The file you are looking for is a 'syntax' file. Try looking in /usr/local/share/vim/syntax/
or /usr/share/vim/syntax/
. The file you want is xml.vim.
The simplest thing to do is find the line that has
syn keyword xmlTodo contained TODO FIXME XXX
and change it to:
syn keyword xmlTodo contained TODO FIXME XXX Question
This will add the 'Todo' highlight to any comment containing the text Question
.
If you are going to use this for (ever), it would be best to copy the xml.vim into your local ~/.vim/syntax
so that changes to vim won't overwrite your custom syntax file.
Update
If you want a similar (sortof) capability in any file, I would use the 'goto' command. I'll admit that this is like hlsearch, which you said annoys you.
While editing, when the cursor is on a word, type gd
to search from the current point in the file or gD
to search from the beginning. Every word in your file that matches the current word is highlighted. You can type n
to jump to the next instance of that word.
I tend to do this, because you get highlight and navigation with minimal typing.
You turn off the highlight in the usual way: :noh[lsearch]