你会如何标记缓冲区中与其他行完全重复的所有行?通过标记它们,我的意思是突出显示它们或添加角色或其他东西.我想保留缓冲区中行的顺序.
之前:
foo
bar
foo
baz
Run Code Online (Sandbox Code Playgroud)
后:
foo*
bar
foo*
baz
Run Code Online (Sandbox Code Playgroud)
ram*_*ion 82
作为ex one-liner:
:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch
Run Code Online (Sandbox Code Playgroud)
这使用该Repeat组突出显示重复的行.
打破它:
syn clear Repeat ::删除以前发现的任何重复g/^\(.*\)\n\ze\%(.*\n\)*\1$/ ::对于稍后在文件中重复的任何行
^\(.*\)\n ::全线\ze ::匹配结束 - 验证模式的其余部分,但不要使用匹配的文本(正向前瞻)\%(.*\n\)* ::任意数量的实线\1$ ::匹配实线的整行重复exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"'::将与此匹配的完整行添加到Repeat语法组
exe ::执行给定的字符串作为ex命令getline('.') ::当前行匹配的内容 g//escape(..., '".\^$*[]') ::使用反斜杠转义给定的字符以制作合法的正则表达式syn match Repeat "^...$"::将给定的字符串添加到Repeat语法组nohlsearch ::从搜索中删除突出显示 g//贾斯汀的非正则表达式方法可能更快:
function! HighlightRepeats() range
let lineCounts = {}
let lineNum = a:firstline
while lineNum <= a:lastline
let lineText = getline(lineNum)
if lineText != ""
let lineCounts[lineText] = (has_key(lineCounts, lineText) ? lineCounts[lineText] : 0) + 1
endif
let lineNum = lineNum + 1
endwhile
exe 'syn clear Repeat'
for lineText in keys(lineCounts)
if lineCounts[lineText] >= 2
exe 'syn match Repeat "^' . escape(lineText, '".\^$*[]') . '$"'
endif
endfor
endfunction
command! -range=% HighlightRepeats <line1>,<line2>call HighlightRepeats()
Run Code Online (Sandbox Code Playgroud)
为什么不使用:
V*
Run Code Online (Sandbox Code Playgroud)
在正常模式下。
它只是搜索当前行的所有匹配项,从而突出显示它们(如果启用该设置,我认为这是默认设置)此外,您可以使用
n
Run Code Online (Sandbox Code Playgroud)
浏览比赛