How to sort lines in Vim
Type :sort and press ↵ to sort every line in the file A to Z. :sort u also removes duplicate lines, :sort n sorts by number and :sort! sorts backwards. Select lines with V first to sort only those.

Tomorrow's market run for Byte Burrito is a list everyone added to as things ran out, so it's in the order people remembered them. You want it A to Z before you leave, so you can read down it in the shop.
Press : in Normal mode to open the command line at the bottom of the screen, type sort and press ↵. With nothing in front of it, :sort takes every line in the file. If the result isn't what you wanted, u puts the lines back where they were.
tortillaslimescilantroavocadosonionslimesblack beansavocadosblack beanscilantrolimeslimesonionstortillasSort the market list
Put the list in A to Z order.
:sort and press ↵market.txtThis editor needs a keyboard. Read on, and play it at your computer.
Remove duplicate lines
Add u (for unique) and Vim sorts as before, then keeps only the first of each run of identical lines. Identical means every character matches. limes and Limes are two different lines to Vim, and so is limes with a space after it. Add i as well, :sort ui, and lines that differ only in case count as the same.
One of each
The list is sorted, and limes are on it twice.
:sort u and press ↵market.txtThis editor needs a keyboard. Read on, and play it at your computer.
Sort numbers by their value
Tonight's online orders came in out of order, and you want them by order number. Plain :sort compares lines a character at a time, the way a dictionary does, so order 112 lands above order 98 because 1 comes before 9.
Without the n
The cursor is on the first line, in Normal mode. You type :sort and press ↵. Which order is on top now?
Add n and Vim reads the first number on each line and sorts by its value, smallest first. A line with no number in it counts as smaller than any number, so a heading goes to the top. For the biggest first, put ! straight after sort, as in :sort! n. The ! reverses any sort, so :sort! on its own gives Z to A.
Orders by number
Put the orders in order, then turn them round so the newest is on top.
:sort n and ↵orders.txtThis editor needs a keyboard. Read on, and play it at your computer.
Sort only some lines
In code you rarely want the whole file sorted. In the shop's main file only the imports need it. Sort everything and the blank line jumps to the top, with the export line right under it.
To sort a few lines, select them first. Press V to select the line the cursor is on, stretch the selection with j, then press :. Vim fills in :'<,'>, which means the lines you selected, and you type sort after it. Line numbers work too. :2,$sort sorts from line 2 to the end and leaves a heading on line 1 where it is.
Sort the imports
The cursor is on the first import.
:, type sort and press ↵shop.jsThis editor needs a keyboard. Read on, and play it at your computer.
Sort by something later on the line
Plain :sort compares lines from their first letter. To sort on a later part of each line, give it a pattern between slashes. Vim finds the pattern on each line and sorts on whatever comes after it.
The kitchen wants tonight's orders grouped by dish so it can make all the burritos together. Every line reads order 112: 3 burritos, and the dish comes after the colon, a space, the quantity and another space. The pattern : \d\+ matches exactly that stretch (\d\+ is one or more digits), so :sort /: \d\+ / sorts on the dish. Lines that tie, like the two burrito orders, keep the order they were in.
Group the orders by dish
The cursor can be anywhere in the file.
:sort /: \d\+ / and press ↵dojo.tsThis editor needs a keyboard. Read on, and play it at your computer.
| Type | Sorts |
|---|---|
:sort ↵ | Every line in the file, A to Z. |
:sort! ↵ | Every line, Z to A. |
:sort i ↵ | A to Z, ignoring case. |
:sort u ↵ | A to Z, keeping one of each set of identical lines. |
:sort ui ↵ | The same, with Limes and limes counted as one line. |
:sort n ↵ | By the first number on each line, smallest first. Lines with no number go on top. |
:sort! n ↵ | By number, biggest first. |
:sort /pattern/ ↵ | On what comes after the pattern on each line. Lines without it go on top. |
:sort /pattern/ r ↵ | On the text the pattern matches, such as :sort /\d\+$/ r n for the number at the end of each line. |
:sort f ↵ | By a decimal number at the start of each line (8.5 before 8.75). |
:'<,'>sort ↵ | Only the lines you selected (press : in Visual mode and Vim types the '<,'> for you). |
:5,12sort ↵ | Only lines 5 to 12. |
:2,$sort ↵ | From line 2 to the end, leaving line 1 alone. |
| u | Undoes the sort. |