Sort and Reorder Lines

:sort with flags handles unique, numeric, reverse, and key-based sorting.

Keys: :sort

:sort sorts lines lexicographically. Flags switch on numeric, ignore-case, unique, and reverse modes. /pattern/ as a key sorts by the post-pattern portion of each line.

:sort sorts a range of lines in place. With no range, the whole file. Combine flags to get the exact ordering you want without leaving the editor.

Flags

Command Effect
:sort Sort the whole file lexicographically
:sort u Unique โ€” drop duplicate lines after sorting
:sort i Ignore case
:sort n Numeric (treat lines as numbers)
:sort r Reverse
:sort un Unique + numeric
:'<,'>sort Sort the visual selection
:10,20sort Sort lines 10โ€“20
:sort /^\s*/ Sort by the part after leading whitespace
:sort! r /,/ Reverse-sort by the comma-separated key

See also: Shell Commands ({key::!}), Global ({key::g}) and Vglobal ({key::v})