I am trying to achieve the following. If I have a file, e.g. a tex document and I want to have the following with commenting characters (% symbols) highlighted:

    Some text and end of the sentence.
    Some more text.

I already know how to highlight commented lines, but that is not exactly what I want.

Also, I can achieve the desired effect if I just search for the % symbol, but I am too lazy to do that every time after I search anything else.

I also would like to have a config so that in other languages I would have the same (e.g. -- symbols for lua, or # symbol for bash)

Does anybody know how to do that elegantly in my .vimrc file?

  • EDIT1: I just wanted to add, that I would like to know whether it is possible to abstract things in vim. I wanted to have a simple solution where I search for a comment char, and not for the percent sign. Something like /${CommentChar} and not just /% Somehow I start to think that I want too much and the easiest way to do this is just using a macro Sudipta Chatterjee suggested. – gns-ank Aug 17 '11 at 9:46

You can use a Vim macro.

In the command mode, use press q to enable macro mode, and let's call your search highlight a macro 's' - meaning go ahead and press 's'. Now you can search for all the '%' characters by first setting :set hlsearch (just to make sure) and then entering /%. Then, you can save this macro by pressing q again. After this, whenever you enter @s in the command mode you will get your search characters highlighted.

You can save the above macro into your .vimrc file by adding this:

let @s='/%'

You can also enable the search character as a macro argument for different file types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.