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.
Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.
Using the shuf Command The shuf utility is a member of the GNU Coreutils package. It outputs a random permutation of the input lines. The shuf command will load all input data into memory during the shuffling, and it won't work if the input file is larger than the free memory.
You could go "UNIX style" and use the shuf
command from the coreutils
package:
:10,20!shuf<CR>
osx now includes a -R
flag in sort, so you should be able to just use
:'<,'>!sort -R
Sort does not have a -R flag in osx, so:
brew install coreutils
select lines in vim, and run:
:'<,'>!gsort -R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With