Skip to main content
33 votes
Accepted

How to sort case-insensitive?

:sort i - sorts case-insensitive.
niekas's user avatar
  • 1,705
10 votes
Accepted

Sort every few lines

The easy way: join the lines in the logical blocks before sorting. In detail: mark the lines join the indented lines to the lines above: :'<,'>s/\n /^A/ mark the lines again: gv sort them: :'&...
Sato Katsura's user avatar
  • 4,019
10 votes
Accepted

How to sort lines by the right most characters?

If you can use the rev command, you could reverse each line, sort and reverse again, using external commands in a pipe: %!rev | sort | rev This can be done entirely in Vimscript, but it's way more ...
muru's user avatar
  • 25.1k
9 votes
Accepted

Sorting a column without altering other columns

I think your best hope is the vis.vim plugin. This plugin provides a command B which allows to apply a command to a block. Here after installing the plugin, you'd select your block and then use: :'&...
statox's user avatar
  • 50.4k
8 votes
Accepted

How can I sort multiline blocks in vim?

Peter Rincker's SortGroup is the go-to script for this kind of work. https://gist.github.com/PeterRincker/582ea9be24a69e6dd8e237eb877b8978 Somewhat differently to vim's :sort, it takes a pattern ...
Mass's user avatar
  • 14.2k
8 votes
Accepted

How to avoid 'Backwards range given' warning when invoking search range?

In :<from>,<to>command, both <from> and <to> are relative to the current line. What you want is to make <to> relative to <from>. For this you need :help :;: :/foo/...
romainl's user avatar
  • 41.8k
6 votes

Sorting superscript numbers

You can sort these symbols properly by using collation locale sort (modifier l): :%sort l For this to work, you need to have language collate setting set properly, for example: :language collate ...
Mass's user avatar
  • 14.2k
5 votes
Accepted

Sort output of let or set

You can capture output with execute() function and then pump it through sort(). The whole command could be then: command! -bang -nargs=1 -complete=command Sorted \ echo join(sort(split(execute(&...
Matt's user avatar
  • 21.3k
4 votes
Accepted

Cannot sort my bash functions correctly

Try to add the negative offset -1 to the second line specifier in the range passed to the first substitution command: v :g/^function /,/^}/- s/\n/@@@ :sort /^function / :%s/@@@/\r/...
user938271's user avatar
  • 5,997
4 votes

Sort every few lines

Idea: Let's join the n number of lines under case to a single line. Follow the process for all cases. Then, use sort to sort the cases. Then, cut those lines back and indent them. Steps: 1) Join n ...
SibiCoder's user avatar
  • 3,442
4 votes
Accepted

Is there an efficient way to sort a selection of comma separated values in a single line?

Vanilla Vim: Via Expression Register And now a solution for vanilla vim: Position cursor on list ciW (removes unsorted list and puts it into register ") <C-r>=join(sort([<C-r>"]), ',')&...
Hotschke's user avatar
  • 4,830
4 votes

How can I sort multiline blocks in vim?

The standard trick for doing this is to convert your blocks into single lines not by removing the linebreaks, but instead by replacing them with something you can find again easily after the sort. e....
Rich's user avatar
  • 32.4k
4 votes
Accepted

How do I sort lines after a prefix?

You want to sort starting with the text that follows the opening numbers? So use a pattern that matches the numbers (and period and following whitespace)... :sort /^\d\+\.\s*/ Note lack of r flag. ...
B Layer's user avatar
  • 20k
4 votes

How can I sort multiline blocks in vim?

There is no direct functionality for that. (At least non I know of.) Assuming that none of the field short, long and extra contains any line-breaks: First join the lines into a single line. g/^\\...
Ralf's user avatar
  • 9,337
4 votes

Sorting superscript numbers

Because the superscript numbers are not numbers. They are symbols. Let's assume you are using unicode, then the symbols are represented by the hexadecimal codes 0x00b2, 0x00b3, 0x00b9, 0x8304, 0x8308. ...
Karl Yngve Lervåg's user avatar
4 votes

Ignoring the first line of a csv file when sorting in Vim

You can do: :2,$!sort -t ',' -k9,9 -k8,8 -k10n -k2n -k3n -k4n -k5n -k6n -k7n
Vivian De Smedt's user avatar
3 votes

Sorting by the first Uppercase letter doesn't seem to work

Commands such as :sort /[^A-Z]*/ to skip non-uppercase letters should typically work, except when 'ignorecase' is set, which makes that pattern case-insensitive and will skip characters other than any ...
filbranden's user avatar
  • 29.6k
3 votes
Accepted

Sorting by the first Uppercase letter doesn't seem to work

The sort pattern is skipped, so you could use sort /.\{-}\ze\u/ or sort r /\u\+/ (the r should cause it sort on the matching parts.) Importantly, any lines not matching are kept in order, but ...
D. Ben Knoble's user avatar
  • 27.2k
3 votes
Accepted

How to sort lines while preserving comment association

Visually select your lines, then run: " join the comments and the related command *g/^\s*#/,/^\s*[^# ]/-s/\n/\="\x01" " sort the lines *sort " split back the lines *s/\%x01/\...
user938271's user avatar
  • 5,997
3 votes
Accepted

Syntax clarification on a global sort command

Add space between pattern, cmd: g /\v\{/ +1,/\v}/-1 sort ^------------------separation between pattern and command It's a good habit to leave space between pattern and cmd if your cmd takes a ...
dedowsdi's user avatar
  • 6,318
3 votes
Accepted

Retrograde sorting

Vim's :sort command can take an optional regex to indicate where to start sorting. From :help :sort: When /{pattern}/ is specified and there is no [r] flag the text matched with {pattern} is ...
filbranden's user avatar
  • 29.6k
3 votes
Accepted

Why does this vim command fail?

For certain commands the | command separation operator doesn't work the way you expect. See :h :bar for the full list. Notice that :global is included. So what's actually happening? The second :g and ...
B Layer's user avatar
  • 20k
3 votes

Is there an efficient way to sort a selection of comma separated values in a single line?

Plugin sort-motion.vim IMO the most convenient method to achieve this is to use a plugin or a self-written vim function linked to a mapping. Both need a customized vim and are obviously not available ...
Hotschke's user avatar
  • 4,830
3 votes

Sorted TODO list with :sort n and letters

If your list is already sorted with :sort n, you can move the done D items to the bottom of the list with a global command: :g/^D/m$ This works through the file from top to bottom, moving each line ...
Rich's user avatar
  • 32.4k
3 votes
Accepted

Sorted TODO list with :sort n and letters

Try adding this to your .vimrc: nnoremap <leader>td :sort n<CR>:sort n /[D]/<CR> Then in Normal mode, you can type \td (assuming you haven't changed the default leader key to ...
Kevin's user avatar
  • 156
3 votes
Accepted

How can I sort a file by word frequency?

Normaly, I would use :%! sort | uniq -c | sort -nr but this is a shell depended answer. Using only Vim language or ex-commands in Vim is a little bit more difficult and it is very nicely described ...
ryuichiro's user avatar
  • 1,171
3 votes

How do I sort lines based on a text property?

This solution uses a simple trick; instead of moving lines you care about to the top, move the other lines to the bottom. This preserves order without any extra book-keeping and allows us to use the ...
Mass's user avatar
  • 14.2k
3 votes

How to shuffle a word list?

In Case you are using vi with Linux , you can use shuf command to "Permute" or "Shuffle" the lines , which are the technical terms to randomize the lines. When the whole file ...
Prem's user avatar
  • 208
3 votes
Accepted

Sort list of citations by the year at the end of the key

Ignore anything that's not a number You can use :sort n that will ignore anything until the first (decimal) number as described in :help sort: vi{ :'<,'>sort n I noticed this will even sort the ...
Friedrich's user avatar
  • 2,795
3 votes
Accepted

How to sort the lines by numbers inside square brackets?

Reading :help :sort and scrolling a bit down, we find (emphasis mine): When /{pattern}/ is specified and there is no [r] flag the text matched with {pattern} is skipped, so that you sort on what ...
Friedrich's user avatar
  • 2,795

Only top scored, non community-wiki answers of a minimum length are eligible