2

I'm on Ubuntu 18.04, vim-gtk3 8.0.

I'm trying to get gvim to highlight multiple lines of comment that starts with a particular word. For example, consider the word 'NOTE':

// NOTE(Ryan): This would be highlighted,
// So would this line
// And this line

// This line would not be highlighted

My current attempt looks like this:

syntax region NoteRegion start=#//\s*NOTE#hs=s+2 end=#\n[^/][^/]#he=s-1
highlight NoteRegion guifg=Red

What I intend this to do is start a region with a // followed by any number of whitespace and then the word NOTE. The highlighting would start after the //, hence the hs=s+2 (I know this won't work for consecutive lines). The region would end with a new line followed by any two characters that are not /. To avoid highlighting these last two characters I have the he=s-1.

Unfortunately this does not work.

Any suggestions would be greatly appreciated.

EDIT: To avoid my highlights being overwritten, my new attempt is:

augroup CommentMarkers
  autocmd!
  autocmd Syntax * syntax region NoteRegion start=#//\s*NOTE# end=#\n(\n|.|[^/][^/])#he=s-1
augroup END
highlight NoteRegion guifg=Red

2 Answers 2

0

Your code isn't far off at all. The problem is that your end marker is a newline followed by two characters which aren't /, and this doesn't appear in your file anywhere!

One fix would be to end instead on any line that is empty or that starts (after whitespace) with any non-/ character:

syntax region NoteRegion start=#//\s*NOTE#hs=s+2 end=#^\s*\($\|[^/\t ]\)#he=s-1

If you don't want any of your comment markers highlighted, one simple fix is to use two groups only one of which is highlighted:

syntax region NoteRegion start=#//\s*NOTE# end=#^\s*\($\|[^/\t ]\)#
syntax match NoteHighlight #//\zs.*# containedin=NoteRegion contained
highlight NoteHighlight guifg=Red
0

I think this will do what you want, except that the highlighting includes the initial //. I don't know any way to do that; adding /s+2 to the end of the search pattern positions the cursor on the 3rd character:

/\/\/\s\+NOTE.*\n\(\/\/\s\+.*\n\)\+

But running a preliminary :global command will produce the highlighting you want, thus:

:%g@//\s\+NOTE@/\s\+NOTE.*\n\(\/\/\s\+.*\n\)\+

I've used @ signs on the :global command to reduce the number of confusing \s a bit. Explanation: the :global (:g) command does a preliminary search for all lines beginning // NOTE. The secondary search (/\s+NOTE etc)is carried out only on the results produced by :g, and does not highlight the // because they are not included in its search pattern. See :h :g for full details.

3
  • I'm not exactly sure as to how you intended for this regex to be used in vim command. On my own initiative, using it like: syntax match NoteRegion "/\/\/\s\+NOTE.*\n\(\/\/\s\+.*\n\)\+" does not work unfortunately. Jun 19, 2020 at 6:50
  • @RyanMcClue you probably need to remove the first slash (which starts a search) and, given that youre using quotes as delimiters, remove the escapes on the slashes only
    – D. Ben Knoble
    Jun 19, 2020 at 11:58
  • @RyanMcClue My apologies, I didn't notice that you use gvim, which I'm really not familiar with. But I do believe gvim does accept Ex commands such as I have used here. Also there is a way to achieve the highlighting you want, by running a preliminary :global command. See the edit of my answer above
    – user985675
    Jun 20, 2020 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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