在 vim 中,如何突出显示 TODO: 和 FIXME:?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您已经发现了问题,但原因如下。
语法匹配分为三种基本类型:关键字、匹配和区域。关键字是固定字符串,通常用于基本语言关键字(
int
、double
、...),在您的情况下,也用于 FIXME 和 TODO。我确实指的是固定弦;它们必须是精确且完整的单词,这与使用正则表达式的匹配和区域不同。例如,从 C 语法来看:几乎所有内置语法定义都是如此,只是组名称不同(cTodo)。
iskeyword
告诉 vim 给定的字符是否可以是关键字的一部分。默认情况下,它不包含冒号,因此在查找关键字时,vim 将“FIXME:”视为“FIXME”,并忽略冒号。如果添加冒号 (set iskeyword+=:
),您现在可以定义额外的突出显示:这取决于您希望如何将其添加到现有语法/突出显示组中。如果它仅适用于一种文件类型,您可以将其添加到该语法的 todo 组(例如 cTodo)。如果您希望它无处不在,您可以按照我的建议执行“myTodo”,然后将其直接链接到 Todo 突出显示组(
hi def link myTodo Todo
)。或者,您可以单独保留
iskeyword
(我可能会推荐这样做),并简单地使用匹配: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: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: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:containedin
会将其添加到所有以“Comment”结尾的组中,加上vimCommentTitle,其中
" TODO: foo
否则不会突出显示为 MyTodo。The
containedin
will add it to all groups ending in "Comment", plusvimCommentTitle, where
" TODO: foo
would not get highlighted as MyTodo otherwise.如果您创建自己的环境,请创建一个语法文件(不是
.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 onesJust add additional syntax in that file. (the way @Jefromi does)