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)iflag — 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), soZebra<apple - With
i,Zebraandzebraare 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 uremoves duplicate lines case-insensitively (soAppleandappleare considered dupes) - Combine with reverse:
:sort! ifor reverse case-insensitive sort - Apply to a range:
:'<,'>sort ito sort only selected lines - Add a numeric flag:
:sort infor case-insensitive numeric sort