I need to match Issue in .conf filetype and highlight it with custom color but set in mode line instead of a fix in syntax/conf.vim file.

The use-case is for each kind of document I need to highlight a specific pattern, Issue in this file, and other may be NOTE

  • 3
    Not possible. You cannot do this using a modeline. Aug 17 '18 at 5:22
  • @ChristianBrabandt Maybe it would be worth to make an answer saying it's not possible to avoid unanswered questions :)
    – statox
    Aug 17 '18 at 11:45

There is no way to do this using modelines, but based on the tag, I assume a solution that involves adding code to your .vimrc is acceptable. If so, you could try something like:

function! s:confhighlight()
    let l:matches = matchlist(getline('$'), '^# highlight: \(.*\)$')
    if len(l:matches)
        let s:matchid = matchadd('Todo', l:matches[1])

function! s:confunhighlight()
    if exists('s:matchid')
        call matchdelete(s:matchid)
        unlet s:matchid

augroup confhighlight
    au BufEnter *.conf call s:confhighlight()
    au BufLeave *.conf call s:confunhighlight()
augroup END

This will search for a line matching the pattern # highlight: foo at the bottom of every .conf file, and highlight foo within that file if such a line is found. (Note that this treats foo as a case-sensitive regex pattern.)

  • Thanks, but it's weird that I can't make it work. I have line contain "test" and the bottom line is # highlight: test guifg=#C792EA. test wasn't highlighted.
    – Tuyen Pham
    Aug 20 '18 at 8:26
  • I got an error: error detected while processing function <SNR>5_confunhighlight: line 2: E803: ID not found: 5
    – Tuyen Pham
    Aug 20 '18 at 8:35
  • 1
    @TuyenPham The function as written only expects a keyword, not a color (e.g. # highlight: test. If you'd like to change the color, you can add highlight Todo guifg=#C792EA to your vimrc as well.
    – Doorknob
    Sep 4 '18 at 3:22
  • How to highlight multiple word, for e.g: # highlight: test abc to highlight both test and abc?
    – Tuyen Pham
    Feb 25 '20 at 8:09

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.