本文介绍了如何在 VI 编辑器中标记/突出显示重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您将如何标记缓冲区中与其他行完全相同的所有行?通过标记它们,我的意思是突出显示它们或添加一个字符或其他东西.我想保留缓冲区中行的顺序.
How would you go about marking all of the lines in a buffer that are exact duplicates of other lines? By marking them, I mean highlighting them or adding a character or something. I want to retain the order of the lines in the buffer.
之前:
foo
bar
foo
baz
之后:
foo*
bar
foo*
baz
推荐答案
作为前任:
:syn clear Repeat | g/^(.*)
ze\%(.*
)*1$/exe 'syn match Repeat "^' . escape(getline('.'), '".^$*[]') . '$"' | nohlsearch
这使用 Repeat
组来突出显示重复的行.
This uses the Repeat
group to highlight the repeated lines.
分解:
syn clear Repeat
:: 删除任何先前发现的重复g/^(.*)ze\%(.*)*1$/
:: 对于文件中后面重复的任何行- 正则表达式
^(.*)
:: 整行ze
:: 匹配结束 - 验证模式的其余部分,但不消耗匹配的文本(正向前瞻)\%(.*)*
:: 任意数量的完整行1$
:: 匹配整行的整行重复
syn clear Repeat
:: remove any previously found repeatsg/^(.*)ze\%(.*)*1$/
:: for any line that is repeated later in the file- the regex
^(.*)
:: a full lineze
:: end of match - verify the rest of the pattern, but don't consume the matched text (positive lookahead)\%(.*)*
:: any number of full lines1$
:: a full line repeat of the matched full line
exe
:: 将给定的字符串作为 ex 命令执行getline('.')
::g//
匹配的当前行的内容escape(..., '".^$*[]')
:: 使用反斜杠转义给定的字符以生成合法的正则表达式syn match Repeat "^...$"
:: 将给定的字符串添加到Repeat
语法组
exe
:: execute the given string as an ex commandgetline('.')
:: the contents of the current line matched byg//
escape(..., '".^$*[]')
:: escape the given characters with backslashes to make a legit regexsyn match Repeat "^...$"
:: add the given string to theRepeat
syntax group
Justin 的非正则表达式方法可能更快:
Justin's non-regex method is probably faster:
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()
这篇关于如何在 VI 编辑器中标记/突出显示重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- the regex
- 正则表达式