33
votes
Accepted
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: :'&...
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 ...
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:
:'&...
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 ...
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/...
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 ...
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(&...
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/...
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 ...
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>"]), ',')&...
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....
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. ...
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/^\\...
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. ...
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
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 ...
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 ...
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/\...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sort × 41regular-expression × 4
vimscript × 3
replace × 3
visual-mode × 2
functions × 2
global-command × 2
multiple-lines × 2
vimrc × 1
search × 1
command-line × 1
substitute × 1
microsoft-windows × 1
macro × 1
quickfix × 1
ex-mode × 1
visual-block × 1
bash × 1
variables × 1
alignment × 1
range × 1
original-vim × 1
delimiter-matching × 1
string-manipulation × 1
user-commands × 1