4

I would like the first line of some documents I have to be highlighted as a comment.

I would like something like:

syntax match myTypeComment /{apply only to first line}^.*$/

But I don't know how this regex should work for only one line (e.g. the first line) in syntax matching.

Thank you very much!

4

You can use the \%1l; this will match a specific line.

For example, to highlight the first line if it starts with # Hello:

:syntax match myTypeComment /\%1l# Hello.*/
:hi myTypeComment ctermfg=red

This also works for other lines (e.g. \%42l for the 42nd line) and you can use \%42<l and \%42>l for lines before or after the 42nd line.

Also see :help /\%l.

3

You can use the \%l regex atom to make sure to match at a particular line. So you would likely want something along the lines of this:

:syntax match myTypeComment /^\%1l#.*/

See also the help at :h /\%l

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.