I am using VIM as a note taking application and have created syntax highlighting to assist with readability.

See below or the repo for all the files.

syntax match Timestamp /^\d\d:\d\d \d\d[/]\d\d[/]\d\d\d\d -/
syntax match Complete /^\/.*$/
syntax match noteSingleDash /^\s*- .*$/ contains=KKS1,noteParenthesis2,noteCurlyBrace2,noteUpUpUp2,noteAt2
syntax match noteDoubleDash /\-- .*$/ contains=KKS1,noteParenthesis2,noteCurlyBrace2,noteUpUpUp2,noteAt2
syntax match noteTripleDash /\--- .*$/ contains=KKS2,noteParenthesis2,noteCurlyBrace2,noteUpUpUp2,noteAt2
syntax match noteQuadruDash /\---- .*$/ contains=KKS1,noteParenthesis2,noteCurlyBrace2noteUpUpUp2,noteAt3

syntax match noteParenthesis  /[(].*[)]/
syntax match noteParenthesis2 /[(].*[)]/ contained

syntax match noteCurlyBrace  /[{].*[}]/
syntax match noteCurlyBrace2 /[{].*[}]/ contained

syntax match noteUpUpUp     /\^/
syntax match noteUpUpUp2    /\^/ contained

syntax match KKS /\a\a\a\d\d\a\a\d\d\d\S*\|\s\n/
syntax match KKS1 /\a\a\a\d\d\a\a\d\d\d\S*\|\s\n/ contained
syntax match KKS2 /\a\a\a\d\d\a\a\d\d\d\S*\|\s\n/ contained

syntax match noteAt /@\w*/
syntax match noteAt2 /@\w*/ contained
syntax match noteAt3 /@\w*/ contained

Is there a better way to apply the highlighting of parentheses, braces, carets, the KKS and "@" regexes without creating multiple lines for each?

For example can noteParenthesis be "contained" (and apply to dashed lines) while also effecting regular unhighlighted lines (text following a "Timestamp"?

My .vimrc below:

syntax on
set number
highlight LineNr ctermfg=red
:set wrap linebreak nolist

map <F5> :.! date "+\%H:\%M \%d/\%m/\%Y"<cr>
cmap w!! w !sudo tee > /dev/null %
highlight noteSingleDash ctermfg=cyan guifg=#00ffff
highlight noteDoubleDash ctermfg=magenta guifg=#d700ff
highlight noteTripleDash ctermfg=yellow guifg=#878700
highlight noteQuadruDash ctermfg=red guifg=#af0000
highlight Timestamp ctermfg=green guifg=#005f00
highlight Complete ctermfg=green guifg=#005f00

highlight noteParenthesis ctermfg=red guifg=#af0000
highlight noteParenthesis2 ctermfg=red guifg=#af0000

highlight noteCurlyBrace ctermfg=red guifg=#af0000
highlight noteCurlyBrace2 ctermfg=red guifg=#af0000

highlight noteUpUpUp ctermfg=cyan guifg=#00ffff
highlight noteUpUpUp2 ctermfg=cyan guifg=#00ffff

highlight KKS ctermfg=yellow guifg=#878700
highlight KKS2 ctermfg=magenta guifg=#d700ff
highlight KKS1 ctermfg=yellow guifg=#878700

highlight noteAt ctermfg=red guifg=#af0000
highlight noteAt2 ctermfg=red guifg=#af0000
highlight noteAt3 ctermfg=cyan guifg=#00ffff


You don't need the syntax definition lines with contained.

Every syntax item could be contained in another syntax item. You need contained if a syntax item should only be detected when it is contained in another item. (:help :syn-contained)

So this should work:

syntax match noteSingleDash /^\s*- .*$/ contains=KKS,noteParenthesis,noteCurlyBrace,noteUpUpUp,noteAt

You might want to check contains=ALL. With this a syntax item might contain any other item. Like:

syntax match noteSingleDash /^\s*- .*$/ contains=ALL

Another way is to work with cluster. A cluster is a group of syntax items that can be referenced by name. See :help :syn-cluster.

syntax match noteSingleDash /^\s*- .*$/ contains=@FlowText

syntax match noteParenthesis  /[(].*[)]/
syntax match noteCurlyBrace  /[{].*[}]/
syntax match noteUpUpUp     /\^/
syntax match KKS /\a\a\a\d\d\a\a\d\d\d\S*\|\s\n/
syntax match noteAt /@\w*/

syntax cluster FlowText contains=KKS,noteParenthesis,noteCurlyBrace,noteUpUpUp,noteAt

BTW: noteSingleDash requires the dash to be the first non-whitespace character on the line. The other *Dash could be anywhere in the text.

| improve this answer | |
  • 1
    Thanks! This is just what I was after, I hadn't noticed the issue with the other *Dash matches either! – xvk3 Oct 12 '19 at 11:03

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.