PERL script, to invoked from within VIM, to adjust
text files for up 80 characters per line.
-
Sometimes it is required to adjust text files to up to 80 characters per line.
I have this need every time, that I import files from out-look for example.
-
To run the script on the entire file, type:
:%!perl ~/bin/vim_up_to_80_char.pl
Or mark a text area, using VIMmark commands say:'a and 'b and type:
:'a,'b!perl ~/bin/vim_up_to_80_char.pl
The script is shown below:
#!/bin/perl
while (<STDIN>) {
chomp($_); $line=$_;
$len=length($line);
while( $len > 80 ) {#split on space
$loc=index($line, ' ');
$loc_last=$loc;
while( $loc < 80 && $loc != -1 ) {
#print("$line\n$loc\n\n");
$loc_last=$loc; $loc++;
$loc=index($line, ' ', $loc);
}
if($loc_last != -1) {
$tmp=substr($line, 0, $loc_last);
print("$tmp\n");
$loc_last++;
$line=substr($line, $loc_last);
$len=length($line);
} else {#can break if there is no space
last;
}
}
print("$line\n");#residual
}
|