Search code examples
vimduplicatesvim-syntax-highlighting

mark duplicate lines and exclude characters from the results


I am using this one-liner to find duplicate lines in a text file:

:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch

When I execute it on C source files, it correctly returns so many { and break; and empty lines. However, they are not the most interesting lines. Howe I can exclude one or multiple characters from the result of this one-liner?


Solution

  • Wow that was hard. But i think i got it:

    :syn clear Repeat | g/^\(^\(\(\s*break\;\|\s*{\|\s*}\)\@!.\).*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch
    

    It ignores the following 3 patterns:

    \s*break;
    \s*}
    \s*{
    

    You can ad more in the list separated by \|.

    As you can easily spot, it isn't really a nice readable solution. You could improve it with the very magic mode see :h \v. But why do you even need the test for 2 same lines? maybe there is a better solution for your whole usecase