Reposted Vim Delete duplicate rows
2 related articles are included below
1) posted from: http://kangzye.blog.163.com/blog/static/36819223201061705729933/
Vim finds the same two rows, and deletes duplicate rows
To find the same two rows, sort the contents first, and then find the previous line equals the last line
: Sort
/^\ (. \+\) $\n\1
So found, the profound vim–. –
Delete duplicate rows (first order):
: Sort
: g/^\ (. \+\) $\n\1/d
2) posted from: http://kangzye.blog.163.com/blog/static/36819223201061705729933/
About the duplicate lines in the deleted text of vim.
Recently began to use Vim, feel this is the artifact of the outside world, no wonder so many vimer to its preference. Now say the text in the reread row delete question. Vim's support for regular is most powerful in many editors, and many advanced regular expressions are supported in VIM. Here's my solution:
:%s/\ (. *\) \n\1/\1\r/g
: g/^$/d
This requires two commands, and then Http://vim.wikia.com/wiki/Uniq_-_Removing_duplicate_lines found my enhanced version:
: g/^\ (. *\) $\n\1$/d
: g/\% (^\1$\n\) \@<=\ (. *\) $/d
Both of the preceding commands are similar to mine, which deletes the previous item of the duplicates, and the subsequent command deletes the contents of the duplicates. Note Two commands are independent and can be used separately. For the G command is not very familiar, it is a global command and will be fully introduced later.
Explanation of the second command:
g//d <–delete The lines matching the regexp
\@<= <–if the bit following matches, make sure the bit preceding this symbol directly precedes the match
\ (. *\) $ <–match The line into subst Register 1
\%( ) <-group without placing in a subst register.
^\1$\n <-match subst Register 1 followed by end of line and the new line between the 2 lines
Vim Delete duplicate rows