Search code examples
listsortingvimrandom

How to shuffle a list in vim?


What is the best way to shuffle a list in Vim?

I'm trying to randomly sort lines in vim (using vimscript). I created for this a list with all line numbers (in my selection).

p.e. if I select from line 10 to 20 my list will be:

mylist = ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']

I would like to sort the lines randomly and with a for loop put them back in the same selection in the new sequence. In order to realize this I would like to shuffle the list many times but don't know how to shuffle the list.

Another solution would be to pick an index from my last randomly.

Can't find out what is the best way.

I know I can sort lines using python, ruby or with other languages but I don't want to install them. I would like to realize above using vimscript.


Solution

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

    :10,20!shuf<CR>