Vim Recall繁體中文

Sort a list and remove duplicate lines

:%sort u

Example

Normal mode. Lines: pear, apple, pear, banana.

Before
pear
apple
pear
banana
  1. :%sort u<CR> Sorted, duplicates removed · apple, banana, pear
After
apple
banana
pear
Details & Vim help

Sort the chosen lines and keep one copy of identical lines.

Clean repeated entries in a scratch list.

Sorting changes line order. u removes consecutive identical whole lines after sorting, ignoring case with i. Equal numeric keys with n do not make different lines duplicates.

Try it in Vim

  1. Run :%sort u<CR> in the example.
  2. Use a smaller line range to preserve headings.
  3. Use :sort n when numeric ordering is needed instead.