Remove Duplicate Line in Vim?

后端 未结 9 1174
有刺的猬
有刺的猬 2021-01-15 06:19

I\'m trying to use VIM to remove a duplicate line in an XML file I created. (I can\'t recreate the file because the ID numbers will change.)

The file looks something

9条回答
  •  半阙折子戏
    2021-01-15 06:54

    It seems like the bash, python and perl methods would work but you are already in vim. So why not create a function like:

    function! RemoveDuplicateLines()
        let lines={}
        let result=[]
        for lineno in range(line('$'))
            let line=getline(lineno+1)
            if (!has_key(lines, line))
                let lines[line] = 1
                let result += [ line ]
            endif
        endfor
        %d
        call append(0, result)
        d
    endfunction
    

提交回复
热议问题