繁体   English   中英

文件第一行的Vim语法区域

[英]Vim syntax region on the first lines of a file

我尝试为笔记创建语法定义。

这是一个例子:

=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

关于即将到来的第一个空白,如何在第一个空行/空白行或文件末尾停止区域?

您可以在模式中为区域end包含表示文件结尾的特殊原子: 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一起提供的各种语法脚本之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM