1

I am working with text files and Goyo. I'd like to highlight two different kinds of notes delimited by brackets (one called "NOTE:" and the other "TODO:") in different colors.

I want to highlight everything within the brackets not just the words "NOTE" or "TODO". Below is the code I have in my init.vim (I am using Neovim btw). It works fine if I try to highlight only one thing. But if I add line 220 and line 223 below (duplicating the call to highlight and the match) it doesn't highlight anything at all.

How can I highlight different matches ("Notes", "Todos" and anything else I might need later) with different colors?

   0 function! s:goyo_enter()
      212     Pencil
      213     Limelight
      219     highlight noteHi ctermfg=Magenta cterm=italic
    + 220     highlight todoHi ctermfg=Green cterm=italic
      222     match noteHi /\[\cNOTE:[^\]]*\]/
    + 223     match todoHi /\[\cTODO:[^\]]*\]/
      227 endfunction

I have also tried using matchadd() with a regex in quotes but the patterns don't match. If I remove everything leaving only "NOTE" and "TODO" in quote those do get highlighted, but it's not what I want.

I want to highlight everything within the brackets that start with those words. For example, [TODO: Do something here.] would have all the text inside the brackets highlighted.

 226     call matchadd("noteHi", "\[\cNOTE:[^\]]*\]")
 227     call matchadd("todoHi", "\[\cTODO:[^\]]*\]"
2

About :match

With the match command, you only get 3 match ids:

:match noteHi /\[\cNOTE:[^=]*\]/
:2match todoHi /\[\cTODO:[^=]*\]/
:3match what /ever/

Each time you call these commands for a particular id (1,2,3) you replace whatever was there before. So you can have 3 different ones but that's it, only 3. If you need more, use matchadd().

About matchadd()

The only problem with your code is that you use double quotes, in which backslashes must be escaped. Simply use single quotes when dealing with patterns.

  call matchadd("noteHi", '\[\cNOTE:[^=]*\]')
  call matchadd("todoHi", '\[\cTODO:[^=]*\]')

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.