Sort lines of textλ︎
Neovim has a built-in :sort command, to sort selected lines of text.
By default lines are sorted in ascending order, although can be sorted in descending order, by numeric values and many other options.
:help sort for the built-in documentation.
All linesλ︎
Sort All Lines in the current buffer in ascending order.
Selected Linesλ︎
Shift+v to visually select one or more lines from the current cursor position.
:sort followed by Enter arranges lines in ascending order.
:sort! followed by Enter arranges lines in descending order.
Unique linesλ︎
Shift+v to visually select one or more lines from the current cursor position.
:sort u followed by Enter arranges lines in ascending order, removing any duplications.
:sort! u followed by Enter arranges lines in descending order, removing any duplications.
NOTE:
uniqbuilt-in command also removes duplicate lines adjacent to each other.
Sort Optionsλ︎
You can customize the sorting behavior with various flags:
!Sort in descending orderuunique sort, remove duplicate lines during sortiIgnore case during sortnSort by numeric valuesfSorts by floating-point numbersxSorts by hexadecimal numbersbSorts by binary numbersoSorts by octal numbers
Options n, f, x, o, and b are mutually exclusive.
Neovim Pluginsλ︎
sQVe/sort.nvim sorts by delimiters e.g. commas, pipes, colons, etc. Sort strings with numbers, e.g., "item1", "item2", "item10").