Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
vim:如何语法高亮显示一行,使其一部分格式化为a,另一部分格式化为B?_Vim_Syntax Highlighting - Fatal编程技术网

vim:如何语法高亮显示一行,使其一部分格式化为a,另一部分格式化为B?

vim:如何语法高亮显示一行,使其一部分格式化为a,另一部分格式化为B?,vim,syntax-highlighting,Vim,Syntax Highlighting,今天我已经在这里工作了几个小时,在线研究并阅读vim手册。我快不知所措了。我想格式化带有时间戳的行,以便它们有绿色文本,同时加粗时间戳本身。例如,如果我有以下4行: 1 [ 20:42:57 20190601 ] Apple car truck a whole bunch of other nonsense 2 ball baby zebra more nonsense 3 [ 20:43:12 20190601 ] dog blah blah blah 4 circle mouse r

今天我已经在这里工作了几个小时,在线研究并阅读vim手册。我快不知所措了。我想格式化带有时间戳的行,以便它们有绿色文本,同时加粗时间戳本身。例如,如果我有以下4行:

1  [ 20:42:57 20190601 ] Apple car truck a whole bunch of other nonsense
2  ball baby zebra more nonsense
3  [ 20:43:12 20190601 ] dog blah blah blah
4  circle mouse rat up down left right b a b a select start
然后,包含时间戳的两行(第2行和第4行)都将有绿色文本,时间戳本身(
[20:42:57 20190601]
[20:43:12 20190601]
)将是粗体

我的第一个想法是使用一个正则表达式模式来匹配所有带有时间戳的行,并将它们涂成绿色,然后使用另一个正则表达式模式来匹配时间戳本身,并将它们加粗,如下所示:

syntax match timestampline "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \].*$"
highlight timestampline ctermfg=green ctermbg=NONE
syntax match timestamponly "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]"
highlight timestamponly cterm=bold
syntax match timestampline "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]\{-}\zs.*$"
highlight timestampline ctermfg=green ctermbg=NONE
syntax match timestamponly "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]\ze"
highlight timestamponly ctermfg=green ctermbg=NONE cterm=bold
但这只会导致时间戳被粗体显示,没有绿色文本

然后我想也许我需要告诉每个语法在哪里停止或开始匹配,就像这样:

syntax match timestampline "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \].*$"
highlight timestampline ctermfg=green ctermbg=NONE
syntax match timestamponly "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]"
highlight timestamponly cterm=bold
syntax match timestampline "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]\{-}\zs.*$"
highlight timestampline ctermfg=green ctermbg=NONE
syntax match timestamponly "\[ \([0-9]\{2}\:\)\{2}[0-9]\{2} [0-9]\{8} \]\ze"
highlight timestamponly ctermfg=green ctermbg=NONE cterm=bold
但这只会导致时间戳是绿色和粗体的,而其他所有内容都是未格式化的


我不明白我做错了什么。为什么第二个突出的陈述完全否定了第一个?它们不应该只是格式化它们匹配的内容,而不影响它们不匹配的内容吗?

:help:syn priority
:

当多个语法项可能匹配时,将使用以下规则:

  • 当多个匹配项或区域项在同一位置开始时,该项 最后定义的具有优先级
  • 因此,在您的代码中,
    timestamponly
    始终获胜,并且从不使用
    timestampline

    您可以通过以下方式获得想要的效果:

    syntax match timestampline /.*$/ contained
    highlight timestampline ctermfg=green
    
    syntax match timestamponly /\[ \d\{2}:\d\{2}:\d\{2} \d\{8} \]/ nextgroup=timestampline
    highlight timestamponly ctermfg=green cterm=bold
    

    :帮助:同步优先级

    当多个语法项可能匹配时,将使用以下规则:

  • 当多个匹配项或区域项在同一位置开始时,该项 最后定义的具有优先级
  • 因此,在您的代码中,
    timestamponly
    始终获胜,并且从不使用
    timestampline

    您可以通过以下方式获得想要的效果:

    syntax match timestampline /.*$/ contained
    highlight timestampline ctermfg=green
    
    syntax match timestamponly /\[ \d\{2}:\d\{2}:\d\{2} \d\{8} \]/ nextgroup=timestampline
    highlight timestamponly ctermfg=green cterm=bold
    

    这太棒了,谢谢你!维姆的事情太多了,我想都不敢想。这太棒了,谢谢你!维姆的事情太多了,我都想不起来了。