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.
Before:
foo
bar
foo
baz
After:
foo*
bar
foo*
baz
As an ex one-liner:
This uses the
Repeatgroup 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^\(.*\)\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 lineexe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"':: add full lines that match this to theRepeatsyntax 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 theRepeatsyntax groupnohlsearch:: remove highlighting from the search done forg//Justin’s non-regex method is probably faster: