As an ex one-liner:
:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch
This uses the Repeat
group to highlight the repeated lines.
Breaking it down:
syn clear Repeat
:: remove any previously found repeatsg/^\(.*\)\n\ze\%(.*\n\)*\1$/
:: for any line that is repeated later in the file- the regex
^\(.*\)\n
:: a full line\ze
:: end of match – verify the rest of the pattern, but don’t consume the matched text (positive lookahead)\%(.*\n\)*
:: any number of full lines\1$
:: a full line repeat of the matched full line
exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"'
:: add full lines that match this to theRepeat
syntax groupexe
:: 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
- the regex
nohlsearch
:: remove highlighting from the search done forg//
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()