Wikia

Vim Tips Wiki

Changes: Uniq - Removing duplicate lines

Edit

Back to page

(See also: add 1166)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 3: Line 3:
 
|previous=647
 
|previous=647
 
|next=649
 
|next=649
|created=February 1, 2004
+
|created=2004
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Michael Geddes
 
|author=Michael Geddes
Line 42: Line 42:
 
==See also==
 
==See also==
 
*[[VimTip1148|Unique sorting]] script to 'sort unique' a List (not text lines)
 
*[[VimTip1148|Unique sorting]] script to 'sort unique' a List (not text lines)
*[http://code.google.com/p/lh-vim/source/browse/system-tools/trunk/plugin/system_utils.vim system_utils.vim] command to remove duplicate lines in a range (uses <tt>g//d</tt> method)
+
*[http://code.google.com/p/lh-vim/source/browse/system-tools/trunk/plugin/system_utils.vim system_utils.vim] command to remove duplicate lines in a range (uses <code>g//d</code> method)
 
*[[VimTip1166|Sort lines]] how to sort lines
 
*[[VimTip1166|Sort lines]] how to sort lines
   
Line 53: Line 53:
 
</pre>
 
</pre>
   
This is faster. Uses mark <tt>l</tt>.
+
This is faster. Uses mark <code>l</code>.
 
<pre>
 
<pre>
 
:nno \d2 :g/^/kl\|if search('^'.escape(getline('.'),'\.*[]^$/').'$','bW')\|'ld<CR>
 
:nno \d2 :g/^/kl\|if search('^'.escape(getline('.'),'\.*[]^$/').'$','bW')\|'ld<CR>
Line 59: Line 59:
   
 
----
 
----
Following uses a substitute to delete all repeated lines (leaving only the first line, while deleting following duplicate lines). This is a variation on the <tt>g//d</tt> method.
+
Following uses a substitute to delete all repeated lines (leaving only the first line, while deleting following duplicate lines). This is a variation on the <code>g//d</code> method.
 
<pre>
 
<pre>
 
%s/^\(.*\)\(\n\1\)\+$/\1/
 
%s/^\(.*\)\(\n\1\)\+$/\1/

Revision as of 05:41, July 13, 2012

Tip 648 Printable Monobook Previous Next

created 2004 · complexity intermediate · author Michael Geddes · version 7.0


The following command will sort all lines and remove duplicates (keeping unique lines):

:sort u

If you need more control, here are some alternatives.

There are two versions, the first leaves only the last line, the second leaves only the first line.

g/^\(.*\)$\n\1$/d
g/\%(^\1$\n\)\@<=\(.*\)$/d

Breakdown of the second version:

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

In this simple format (matching the whole line), it's not going to make much difference, but it will start to matter if you want to do stuff like match the first word only.

This does a uniq on the first word in the line, and deletes all but the first line:

g/\%(^\1\>.*$\n\)\@<=\(\k\+\).*$/d

See also

Comments

Here are some more Vim-native ways for removing duplicate lines. This time they don't have to be adjacent. Line order is preserved.

This one can be a bit slow.

:nno \d1 :g/^/m0<CR>:g/^\(.*\)\n\_.*\%(^\1$\)/d<CR>:g/^/m0<CR>

This is faster. Uses mark l.

:nno \d2 :g/^/kl\|if search('^'.escape(getline('.'),'\.*[]^$/').'$','bW')\|'ld<CR>

Following uses a substitute to delete all repeated lines (leaving only the first line, while deleting following duplicate lines). This is a variation on the g//d method.

%s/^\(.*\)\(\n\1\)\+$/\1/

Around Wikia's network

Random Wiki