Removing duplicate rows in vi?

后端 未结 14 2066
无人及你
无人及你 2021-01-29 23:10

I have a text file that contains a long list of entries (one on each line). Some of these are duplicates, and I would like to know if it is possible (and if so, how) to remove

14条回答
  •  花落未央
    2021-01-29 23:59

    This version only removes repeated lines that are contigous. I mean, only deletes consecutive repeated lines. Using the given map the function does note mess up with blank lines. But if change the REGEX to match start of line ^ it will also remove duplicated blank lines.

    " function to delete duplicate lines
    function! DelDuplicatedLines()
        while getline(".") == getline(line(".") - 1)
            exec 'norm! ddk'
        endwhile
        while getline(".") == getline(line(".") + 1)
            exec 'norm! dd'
        endwhile
    endfunction
    nnoremap d :g/./call DelDuplicatedLines()
    

提交回复
热议问题