14

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

6 Answers 6

11

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.*/

8
  • Actually that only sortive worked... I suspect that you are only allowed to have one match at a time see update #3... Feb 18, 2010 at 14:09
  • 1
    @sixtyfootersdude: Sorry, once again there was minor error. All of the match commands have to instead be "syntax match" commands. So instead of [ :match MyQuestions /.*Question.*/ ] you would want to use [ :syntax match MyQuestions /.*Question.*/ ]. That lets you have as many different matches as you want. These are in fact one of the most common types of commands placed in syntax files. But you can execute them anywhere you want, either command line, vimrc, or any other scripts.
    – user22303
    Feb 18, 2010 at 16:23
  • Adding syntax infront of match... in my vimrc file makes match have no effect at all. Is this where I should be doing this. Doing it to an open file also has no effect. Feb 18, 2010 at 18:06
  • 1
    @sixtyfooters: Not sure what's happening for you, but that's definitely the way to do it. Test at command line before placing in a script (it's possible syntax settings are getting cleared somewhere in other scripts that are loaded for your file). Once defined, check what groups exist and what highlighting they have by issuing a bare ":highlight" command (or choosing "Syntax", "Highlight test" from gui menu in gvim). Remember you want to use highlight command first to create group, then use 'syntax match' to set match pattern.
    – user22303
    Feb 18, 2010 at 19:02
  • @sixtyfooters: I'm assuming you're running vim in a terminal (i.e., not gvim) so that setting the term properties only should work. It sounds to me like the syntax commands are getting cleared when a new file is loaded and an associated syntax file is running and clearing out all previous syntax commands. So just to double-check, when using command line are you re-entering these commands every time you load a file? I think you could try testing with text in new buffer that isn't even saved and doesn't even have a filename assigned yet.
    – user22303
    Feb 19, 2010 at 17:59
5

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]

9
  • Wow! That sound like what I want to do. I currently do not have a ~/.vim/ folder. If I created it and create the file syntax` do I need to reference it somewhere (maybe in .vimrc?) to tell vim to use it? Feb 17, 2010 at 17:17
  • 1
    Nope. Vim automatically looks for it (and a few other directories - for example: plugins, color, compiler) and if a syntax file exists in your home area, it takes precedence of the system default. Feb 18, 2010 at 15:20
  • 1
    Also, just to be clear, you want to create a directory syntax and put in it a file named xml.vim Feb 18, 2010 at 15:22
  • Should I put my .vimrc into my .vim dir? Feb 18, 2010 at 17:47
  • 1
    syntax files go in your .vim dir. Syntax highlight is intended to be language specific. If you want a similar (sortof) capability in any file, I would use the 'goto' command. I'll add it to my answer. Feb 18, 2010 at 20:42
2

You could simply do

:set hlsearch

And then search for Question

1
  • That is what I have been doing but hlsearch annoys me. +1, thanks for the suggestion. Feb 17, 2010 at 17:20
2

You have several options.
- one, as Dominik mentioned ... you can search for those one by one (not that practical)
- two, you can do ":g/<-- Question" to get a "list" of them all (a little better, gives an overall picture)
- three, you can set up a highlighting group if you're only using those kind of comments in xml files (I've no idea how xml looks like, so I don't know if there are some other kind of comments in them as well). This would be good if you setted it up in vimrc, since when you refresh vim with a newer version, your xlm.vim file will be gone.

5
  • +1 I like the :g/ suggestion, but might be tough to apply if 'Question' doesn't immediately follow the comment tag. Feb 17, 2010 at 16:32
  • @DaveParillo - ah, yes. well, as i said, i don't know anything about xml so i assumed it was always like so.
    – Rook
    Feb 17, 2010 at 17:11
  • How can I setup a highlighting group? Feb 17, 2010 at 17:19
  • 1
    @sixtyfootersdude - see ":help e410".
    – Rook
    Feb 17, 2010 at 18:42
  • @Idigas: thanks for the reference. Had a look but still could not solve my problem. I added an update in the question. Thanks. Feb 17, 2010 at 19:33
1

You don't necessarily need to put the command in your vimrc. You can always just enter it at command line and then clear it when you don't want the highlighting anymore. Commands would be something like this:

:hi Question guifg=red guibg=blue gui=bold

In gvim (i.e., vim with a gui) that will set the foreground(guifg), background, and general character settings.

if you want to get rid of that higlighting do this:

:hi Question clear

Using vim inside a terminal you can modify to use this command:

:hi Question ctermfg=red ctermbg=blue term=bold

You can of course put any of those in your vimrc, and you can also combine the gui and term flags into single hi command.


[below added after I read another poster's answer]

Sorry, I was assuming that the syntax of your question lines already matched your syntax file's settings for Vim's built-in 'Question' highlight tag. Just look at the other answer that mentions the xml syntax file for vim and then you can issue commands above using ToDo highlight group instead of Question.

2
  • Tried :hi Question ctermfg=red ctermbg=blue term=bold but had no effect. Feb 17, 2010 at 17:14
  • Sorry, the :hi command merely established how the Question group would be highlighted. Now you have to specify what areas of document are part of the Question group. You can do this with a match command: :match Question /[search pattern]/ or, in your case: :match Question /Question/
    – user22303
    Feb 17, 2010 at 23:09
0

I'm late to the party but I use TODO and FIXME for exactly this situation.

Most, if not all, colorschemes will highlight the TODO in the following example with some combination of background color, underline and italics :

<tree>
  <!--TODO : do I have to add a trunk tag ?-->
  <branch>
    <leaf>caterpillar</leaf>
    <leaf>caterpillar</leaf>
  </branch>
</tree>

It doesn't really fit semantically but it works very well from a purely functional point of view.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .