I have a file that has dates in this format: Fri Jun 05, 2015. It's essentially a diary.

I have created the syntax keywords Day, Month, dateNum, and Year for the various components of the date but I want to highlight the date as a single entity. Is it possible to create a syntax rule that is a sequence of keywords?

Here's the contents of my syntax file so far.

syn match Code '`.*`'
syn match Time '[01][0-9]:[0-5][0-9]\(:[0-5][0-9]\)\?'

syn keyword Day Mon, Tue, Wed, Thu, Fri, Sat, Sun, nextgroup=Month
syn keyword Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec nextgroup=dateNum

syn match dateNum '[0-3][0-9],'  nextgroup=Year
syn match Year '\d\{4}'

syn region Code start="`" end="`"

I am manually setting the highlighting for these groups for the time being (e.g. hi Time gui=bold).

Here's a sample diary file. Note that the highlighting for Time and Code already work.

Sun Jun 07, 2015

23:48 Checked out `bleeding-edge` branch.

Mon Jun 08, 2015

09:17 Woke up

09:18 Hit snooze.

Yes; you can just combine the keywords in a long :syntax match sequence. For example:

augroup diary
    autocmd ColorScheme,BufRead,BufNewFile *
        \  syntax match DiaryDate "\v(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d\d, \d\d\d\d"
        \| highlight DiaryDate ctermbg=green guibg=green
augroup END

enter image description here

  • I was hoping that I could use the keywords I already generated. Is that possible? I also don't understand some of the syntax. What is the \v at the start of the match string? And, what does the escaped-pipe, \| mean? Jun 11 '15 at 15:38
  • @Stephen the \v is the [very magic]( vim.wikia.com/wiki/…) regex switch it makes all characters having a special meaning excepted [a-zA-Z0-9] and _
    – statox
    Jun 13 '15 at 9:54

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.