在 vim 中,如何突出显示 TODO: 和 FIXME:?

发布于 2024-10-01 03:13:12 字数 88 浏览 8 评论 0原文

在vim中,FIXME和TODO被突出显示,但我无法让FIXME:和TODO:(注意关键字后面的冒号)突出显示?我应该在 .vimrc 中添加什么才能实现这一点?

In vim, FIXME and TODO are highlighted, but I can't get FIXME: and TODO: (note the colon after the keyword) to highlight? What should I put in my .vimrc to make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云朵有点甜 2024-10-08 03:13:12

好吧,您已经发现了问题,但原因如下。

语法匹配分为三种基本类型:关键字、匹配和区域。关键字是固定字符串,通常用于基本语言关键字(intdouble、...),在您的情况下,也用于 FIXME 和 TODO。我确实指的是固定弦;它们必须是精确且完整的单词,这与使用正则表达式的匹配和区域不同。例如,从 C 语法来看:

syn keyword   cTodo   contained    TODO FIXME XXX

几乎所有内置语法定义都是如此,只是组名称不同(cTodo)。

iskeyword 告诉 vim 给定的字符是否可以是关键字的一部分。默认情况下,它不包含冒号,因此在查找关键字时,vim 将“FIXME:”视为“FIXME”,并忽略冒号。如果添加冒号 (set iskeyword+=:),您现在可以定义额外的突出显示:

syn keyword   myTodo   contained   TODO: FIXME:

这取决于您希望如何将其添加到现有语法/突出显示组中。如果它仅适用于一种文件类型,您可以将其添加到该语法的 todo 组(例如 cTodo)。如果您希望它无处不在,您可以按照我的建议执行“myTodo”,然后将其直接链接到 Todo 突出显示组(hi def link myTodo Todo)。

或者,您可以单独保留 iskeyword (我可能会推荐这样做),并简单地使用匹配:

syn match   myTodo   contained   "\<\(TODO\|FIXME\):"
hi def link myTodo Todo

Well, you've already found the problem, but here's the why.

There are three basic types of syntax matching: keywords, matches, and regions. Keywords are fixed strings, generally used for basic language keywords (int, double, ...) and also, in your case, for the FIXME and TODO. I really do mean fixed strings; they have to be exact and whole words, unlike matches and regions, which use regex. For example, from the C syntax:

syn keyword   cTodo   contained    TODO FIXME XXX

It looks like that in pretty much all built-in syntax definitions, just with different group names (cTodo).

iskeyword tells vim whether a given character can be part of keyword. By default, it does not include colons, so when looking for keywords, vim sees "FIXME:" as "FIXME", and ignores the colon. If you tack on the colon (set iskeyword+=:), you can now define an extra bit of highlighting:

syn keyword   myTodo   contained   TODO: FIXME:

It's up to you how you want to work it into the existing syntax/highlight groups. If it's for just one filetype, you could add it to that syntax's todo group (e.g. cTodo). If you want it everywhere, you can do "myTodo" as I suggested, then link it straight to the Todo highlighting group (hi def link myTodo Todo).

Alternatively, you can leave iskeyword alone (I'd probably recommend this), and simply use a match:

syn match   myTodo   contained   "\<\(TODO\|FIXME\):"
hi def link myTodo Todo
爱殇璃 2024-10-08 03:13:12
augroup vimrc_todo
    au!
    au Syntax * syn match MyTodo /\v<(FIXME|NOTE|TODO|OPTIMIZE|XXX):/
          \ containedin=.*Comment,vimCommentTitle
augroup END
hi def link MyTodo Todo

containedin 会将其添加到所有以“Comment”结尾的组中,加上
vimCommentTitle,其中 " TODO: foo 否则不会突出显示为 MyTodo。

augroup vimrc_todo
    au!
    au Syntax * syn match MyTodo /\v<(FIXME|NOTE|TODO|OPTIMIZE|XXX):/
          \ containedin=.*Comment,vimCommentTitle
augroup END
hi def link MyTodo Todo

The containedin will add it to all groups ending in "Comment", plus
vimCommentTitle, where " TODO: foo would not get highlighted as MyTodo otherwise.

呆橘 2024-10-08 03:13:12

如果您创建自己的环境,请创建一个语法文件(不是 .vimrc

  • 全局语法文件位于 vim 目录(例如
    /usr/share/vim/vim72/syntax/c.vim)

  • 用户语法文件,例如 ~/.vim/syntax/c.vim,覆盖全局语法

只需在该文件中添加其他语法即可。 (就像@Jefromi 所做的那样)

If you make your own environment, create a syntax file (not .vimrc)

  • global syntax file is located vim directory (ex.
    /usr/share/vim/vim72/syntax/c.vim)

  • user syntax files, e.g. ~/.vim/syntax/c.vim, override global ones

Just add additional syntax in that file. (the way @Jefromi does)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文