Consider you find yourself in a situation of the utmost importance where you have been tasked with sorting the following list:

3. Tax Filings Q10
4. Legal Documents Q4
12. Payroll Information Q1
1. Some Important File Q2

Hint: if you are following along, you may want to copy that block and put it into a named register for reuse. Since this is the first vim-tip I’ll just add that you can navigate to the 3. Tax Filings Q10 line, hit Shift+V to enter Visual Line Mode, 3j to select the other lines, then "dy where d is just a named register that I am using so I can remember it with the mnemonic demo. To reuse it, you can put it with "dp.

How might you go about sorting this? Perhaps you think you are pretty clever with the :move command and use it on each line with :m .(+|-)n until it is sorted. Or maybe you’re a prompt engineer and get Claude to do it by highlighting the range and invoking :'<,'>!claude -p "Sort this list -- make no mistakes!". Maybe. But, what if you could instead be a Vim power user and invoke the :sort command on this list?

By default, :sort on that list will only do lexicographic sorting:

:sort
1. Some Important File Q2
12. Payroll Information Q1
3. Tax Filings Q10
4. Legal Documents Q4

Which gets us part way there, but has the unfortunate side effct of giving us PTSD of using Excel and trying to sort a column only to realize that it’s formatted as Text. Thankfully, Vim provides us with an optional n flag to the sort command that sorts by the first numeric value it comes across in each line:

:sort n
1. Some Important File Q2
3. Tax Filings Q10
4. Legal Documents Q4
12. Payroll Information Q1

Hooray! We can go to our boss and let him know that this business-critical task has been taken care of. But, oh no, what’s this? He actually wanted it sorted by the Business Quarter first?! This is where individuals using JetBrains IDEs would throw in the towel and start looking for a new job, not us though, we’re special because we use Vim.

:sort has a pattern argument that skips part of each line when sorting. For us, we can exclude everything up to and including the Q:

:sort n /.*Q/
12. Payroll Information Q1
1. Some Important File Q2
4. Legal Documents Q4
3. Tax Filings Q10

I would be remiss to not take an opportunity to point out that you can also shell out to a native sort utility (gnu sort for me):

:'<,'>!sort -n
1. Some Important File Q2
3. Tax Filings Q10
4. Legal Documents Q4
12. Payroll Information Q1

:'<,'>!sort -n -tQ -k2
12. Payroll Information Q1
1. Some Important File Q2
4. Legal Documents Q4
3. Tax Filings Q10