vimtricks.wiki Concise Vim tricks, one at a time.

How do I sort lines in Vim without caring about upper or lower case?

Answer

:sort i

Explanation

By default, :sort uses byte-value ordering, which places all uppercase letters before lowercase. Adding the i flag makes the sort case-insensitive so that Apple, apple, and APPLE are treated equally.

How it works

  • :sort — sort all lines in the buffer (or a given range)
  • i flag — ignore case during comparison, making uppercase and lowercase equivalent
  • Without i, ASCII uppercase (A-Z = 65-90) always sorts before lowercase (a-z = 97-122), so Zebra < apple
  • With i, Zebra and zebra are compared as if they were the same case

Example

Given these lines:

Zebra
apple
Banana
cherry

Running :%sort (without i) sorts by byte value:

Banana
Zebra
apple
cherry

Running :%sort i sorts alphabetically regardless of case:

apple
Banana
cherry
Zebra

Tips

  • Combine flags: :sort i u removes duplicate lines case-insensitively (so Apple and apple are considered dupes)
  • Combine with reverse: :sort! i for reverse case-insensitive sort
  • Apply to a range: :'<,'>sort i to sort only selected lines
  • Add a numeric flag: :sort in for case-insensitive numeric sort

Next

How do I open the directory containing the current file in netrw from within Vim?