Recently, I needed to perform a random sort of lines in vim. After a little bit of googling, I came up with the following:

(Works on Mac OS X Leopard and ubuntu 9.4)

Create the following perl script file:
#!/usr/bin/perl -w
use strict;
use List::Util 'shuffle';
my @lines = <>;
print shuffle( @lines );

Save the file and make it executable as well as easily accessible. Such as a directory that is in your PATH variable.

Now, fire up vim and visually select the lines you want to randomly select. Now, type:

!randsort.pl

There it is — randomly sorted lines.

3 Responses to “Cool vim random line shuffle trick”

  1. Tim says:

    Look up the Unix command shuf.

  2. nik says:

    nice trick. it’s cleaner and easier to use if you make it a one-liner this way:
    :%!perl -e 'use List::Util 'shuffle'; print shuffle();'

    then you can even put it in ur vimrc and bind it to a key if you want
    e.g.
    map r :%!perl -e 'use List::Util 'shuffle'; print shuffle();'

    then you can just tap ‘\r’ (unless you’re leader key is bound to something different) and vim will reshuffle your file.

    windows users will need to install perl first tho

  3. Kyle Skrinak says:

    Awesome, thanks nik!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Performance Optimization WordPress Plugins by W3 EDGE