我试图为我的笔记创建一个语法定义。
下面是一个示例:
=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
我想创建一个语法来突出显示这些不同的匹配。在我的笔记中,我可以写一些看起来像标题(=方式)或标签(# this -way)的行,它们不需要高亮显示。我试图为从第一行到第一行的注释元数据创建一个区域。
我试过了,但我有问题(高亮显示似乎不错,但如果我删除空行后的所有行(包括空行),那么它不再工作了.
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
如何停止在第一个空/空行或在文件的末尾,对于第一个即将到来的区域?
发布于 2017-02-02 12:34:13
您可以在区域结束的模式中包含表示文件结束的特殊原子:end="^\s*$\|\@$"
;但是,这是不必要的。当start
模式匹配时,Vim总是启动语法区域;它不检查end
模式,正如:help syn-region
解释的那样:
注意:启动区域的决定仅基于匹配的开始模式。没有检查匹配的结束模式。
因此,所有区域都隐式地结束在缓冲区的末尾,即使没有匹配的end
模式。事实上,你的语法对我来说很好。
我认为您之所以感到困惑,是因为您没有定义正确的语法脚本,而是选择了autocmd FileType gtd
前缀。另外,syntax clear
也不见了。
将:syntax
命令放在~/.vim/syntax/gtd.vim
中的单独文件中,并遵循:help 44.12
中描述的格式。您还可以查看$VIMRUNTIME/syntax/*.vim
中随Vim附带的各种语法脚本之一。
https://stackoverflow.com/questions/41946108
复制相似问题