I try to create a syntax definition of my notes.
Here is an example:
=Title of the note (it's the very first line)
@context (mandatory)
!action (mandatory)
#tag-1 (optional)
#tag-2 (optional)
#tag-n (optional)
>attached-files (optional)
My note come after the first blank line
and continue until the end of the file...
No matter any additional empty line
I would like to create the syntax to highlight then theses differents matches. In my note, I can write lines which look like a title (= this way) or a tag (#this-way) and they don't have to be highlighted. I try to create a region for my note metadata going from the first line to the first empty line.
I tried this but I've got issues (highlight seems good but If I delete all lines after the empty line (the empty line included) then it doesn't work anymore...
augroup gtd
autocmd!
autocmd BufRead,BufNewFile *.gtd set filetype=gtd
autocmd FileType gtd syntax region gtdTags start="\%1l" end="^\s*$" fold transparent contains=gtdTitle,gtdContext,gtdStatus,gtdHashtags,gtdAttachedFiles
autocmd FileType gtd syntax match gtdTitle '^=.*' contained
autocmd FileType gtd syntax match gtdContext '^@\S\+$' contained
autocmd FileType gtd syntax match gtdStatus '^!\S\+$' contained
autocmd FileType gtd syntax match gtdHashtags '^#\S\+$' contained
autocmd FileType gtd syntax match gtdAttachedFiles '^>attached-files$' contained
autocmd FileType gtd syntax match gtdSubtitle '^\s*\*\* .*'
autocmd FileType gtd syntax keyword gtdTodo TODO WAITING SOMEDAY SCHEDULED
autocmd FileType gtd highlight gtdTitle guifg=white guibg=NONE gui=bold
autocmd FileType gtd highlight gtdContext guifg=yellow
autocmd FileType gtd highlight gtdStatus guifg=red gui=NONE
autocmd FileType gtd highlight gtdHashtags guifg=grey gui=italic
autocmd FileType gtd highlight gtdAttachedFiles guifg=red guibg=white gui=bold
autocmd FileType gtd highlight gtdSubtitle guifg=black guibg=lightgrey gui=bold
autocmd FileType gtd highlight gtdTodo guifg=white guibg=red gui=NONE
augroup END
How to stop the region on the first empty/blank line or at the end of the file, regarding the first which is coming ?
You could include the special atom representing the end of the file in your pattern for the region end: end="^\s*$\|\@$"
; however, this isn't necessary. Vim always starts a syntax region when the start
pattern matches; it does not check for the end
pattern, as :help syn-region
explains:
Note: The decision to start a region is only based on a matching start pattern. There is no check for a matching end pattern.
So, all regions implicitly end at the end of the buffer, even if there's no match for the end
pattern. And in fact, your syntax works for me just fine.
I think you've got confused because you did not define a proper syntax script, and instead chose the autocmd FileType gtd
prefixes. Also, the syntax clear
is missing.
Place your :syntax
commands into a separate file in ~/.vim/syntax/gtd.vim
, and adhere to the format described at :help 44.12
. You can also have a look at one of the various syntax scripts in $VIMRUNTIME/syntax/*.vim
that ship with Vim.