Learn Practice Daily golf Reference

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.

The ninja sliding clay strips labelled A to D into order on the counter like books on a shelf, a doubled strip dropping into a bin, under a sign reading :sort.

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.

Before
tortillas
limes
cilantro
avocados
onions
limes
black beans
:sort↵
avocados
black beans
cilantro
limes
limes
onions
tortillas
The whole list lines up A to Z, and the two limes now sit together
Try it here

Sort the market list

Put the list in A to Z order.

Type :sort and press ↵market.txt
1tortillas
1limes
2cilantro
3avocados
4onions
5limes
6black beans
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This 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.

Try it here

One of each

The list is sorted, and limes are on it twice.

Type :sort u and press ↵market.txt
1avocados
1black beans
2cilantro
3limes
4limes
5onions
6tortillas
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This 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.

Check yourself

Without the n

The cursor is on the first line, in Normal mode. You type :sort and press ↵. Which order is on top now?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1order 112: 3 burritos
1order 98: 1 bowl
2order 105: 2 tacos
3order 87: 4 burritos
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

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.

Try it here

Orders by number

Put the orders in order, then turn them round so the newest is on top.

Sort by number with :sort n and ↵orders.txt
1order 112: 3 burritos
1order 98: 1 bowl
2order 105: 2 tacos
3order 87: 4 burritos
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This 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.

Try it here

Sort the imports

The cursor is on the first import.

Select the three imports with V and j j, press :, type sort and press ↵shop.js
1import { wrap } from './wrap';
1import { menu } from './menu';
2import { cart } from './cart';
3
4export const shop = createShop(menu, cart, wrap);
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This 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.

Try it here

Group the orders by dish

The cursor can be anywhere in the file.

Type :sort /: \d\+ / and press ↵dojo.ts
1order 112: 3 burritos
1order 98: 1 bowl
2order 105: 2 tacos
3order 87: 4 burritos
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

TypeSorts
: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.
uUndoes the sort.