首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >文件第一行的Vim语法区域

文件第一行的Vim语法区域
EN

Stack Overflow用户
提问于 2017-01-30 21:56:36
回答 1查看 820关注 0票数 1

我试图为我的笔记创建一个语法定义。

下面是一个示例:

代码语言:javascript
运行
复制
=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)的行,它们不需要高亮显示。我试图为从第一行到第一行的注释元数据创建一个区域。

我试过了,但我有问题(高亮显示似乎不错,但如果我删除空行后的所有行(包括空行),那么它不再工作了.

代码语言:javascript
运行
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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附带的各种语法脚本之一。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41946108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档