How to shuffle a list in vim?

if you need a uniformed random number generator, I don't know if vim could provide you one native (or you could turn to some plugins). however, if you just want to break some order of a number of lines, you could try this function:

function! Random()
    return reltimestr(reltime())[-2:]
endfunction

the function above return the last two digits of current timestamp in ms.

to shuf lines, you could (for example the whole buffer)

%s/^/\=Random() . " "

then

:sor n

finally remove added prefix:

%s/^\S* //

You could of course chain the commands above with <bar>.

%s/^/\=Random() . " "/|sor n|%s/^\S* //

or without creating the function, write the random number part in your :s :

%s/^/\=reltimestr(reltime())[-2:] . " "/|sor n|%s/^\S* //

If I test 30 lines with value 1-30 (generated by seq 30),

first time result was:

2
26
12
17
8
22
27
3
13
18
23
9
28
4
14
19
1
24
10
29
5
6
15
20
25
30
11
16
7
21

2nd time result:

4
22
25
28
6
9
18
12
15
1
3
5
21
24
27
30
8
11
17
14
2
20
23
26
29
7
10
16
13
19

hope it helps


You could go "UNIX style" and use the shuf command from the coreutils package:

:10,20!shuf<CR>

UPDATE as of 2019:

osx now includes a -R flag in sort, so you should be able to just use

:'<,'>!sort -R

=== Previous answer: ===

Sort does not have a -R flag in osx, so:

brew install coreutils

select lines in vim, and run:

:'<,'>!gsort -R