Lorem ipsum generator on terminal

If you search for a lorem ipsum generator at the internet, you find a lot of web browser plugins. But what if you program a webpage like me in a normal texteditor (vi — ok, it is not really a normal editor) and you don’t want to switch between browser and editor every time you need some continous text? Therefore I looked for a small lib or programm which I can execute on command line out of vi and get some nice continous text.

After some days of research I found the perl lib Lorem. And with a short wrapper script around it really fits my requirements.

Install the lib

$ sudo apt-get install libtext-lorem-perl

wrapper script

I wrote the following wrapper script around. With this it is possible to switch between words and paragraphs and also set the number of paragraphs/words I want.

#!/usr/bin/env perl
#

use Text::Lorem;
use Getopt::Std;
use File::Basename;

my $me = basename($0);
my %opts;
getopts('pwc:', \%opts);
if (!defined($opts{p}) && !defined($opts{w})){
    printf("usage: %s -p|-w (-c N)\n", $me);
    exit(-1);
}

my $text = Text::Lorem->new();

# default value
my $wordCount = 10;
my $output = null;

if(defined($opts{c})){
    $wordCount = $opts{c};
}

if(defined($opts{p})){
    $output = $text->paragraphs($wordCount);
}

if(defined($opts{w})){
    $output = $text->words($wordCount);
}


print $output;

If you save this code for example in a file called loremipsum.pl, than you can call it like the following example. If you want to have a different default number, you can append on each call -c N and N is your wanted number.

Get paragraphs with default Number (10)

$ perl loremipsum.pl -p

Get words with default Number (10)

$ perl loremipsum.pl -w

comments powered by Disqus