Vim URL Shortener
As the amount of content available online grows, the length of the URL's required to access it seems determined to keep up. This little bundle of a vim script and some Perl code will convert a long URL into a shorter one (using MakeAShorterLink) at the press of a single button.
While the masl.pl script can be used on the command line to shorten URL's if you’re lucky enough to use mutt as your email reader and vim as the editor within it you can easily shorten target addresses so they slip under the magic 75 character limit that differentiates visited URL's from my home pages. But I’m not bitter.
The vim URL shortener comes in two parts, a Perl script and a vim function. Assuming you are running as root and in the untarred directory:
# cp masl.pl /usr/local/bin/
# chmod +x /usr/local/bin/masl.pl
# perl -MCPAN -e 'install WWW::Shorten'
# Copy the contents of shorten.vim to your .vimrc
Long version: the Perl script, masl.pl must be placed in your path and made executable. For example:
cp masl.pl /usr/local/bin/
chmod +x /usr/local/bin/masl.pl
The script itself only requires a single non-core Perl module, WWW::Shorten, which can be installed as per usual. The main options for this are the cpan shell command (which is functionally equivalent to perl -MCPAN -e ‘install WWW::Shorten’), downloading and installing by hand or using your package system.
The vim function contained in shorten.vim needs to be placed in your .vimrc file, this typically lives in your home directory. By default the <F3> key will be mapped to call the shorten function, if this key is already in use simply find and change the following line:
noremap <F3> <Esc>:call ShortenURL()<CR>
And replace <F3> with your own choice of key.
The masl.pl script, by default, allows you to shorten a URL using the engine at MakeAShorterLink. The URL to shorten is passed in as the argument and the new, shorter one if returned on standard out. If anything goes wrong in the process (The URL is too short to shorten, the service is down etc) then the original URL is passed back out allowing the script to be used as a filter.
If invoked with the ‘-l’ option and an already shortened URL the script will return the expanded version. To see all the supported options you can either run the command with ‘-h’ or no arguments.
Once the installation is complete you can access the shortening functionality from within vim by moving the cursor to the URL you want to alter (Anywhere in the URL is fine) and pressing <F3>. Vim will then replace all occurrences of the URL with the new, shorter version before moving the cursor back to its starting position, or as near as it can get on the same line.
The links below allow you to snarf the entire tgz file or any individual file of its contents. While writing this I found peoples online vimrc’s to be excellent help and in all but a few cases easily available. The few that required me to download a tgz, open it, hunt for the one file and then vim it soon became an annoyance. One I hope not to emulate.
- The entire vim shortener bundle. All code and a nice README. Which you should.
- vim shortener function The vim code to call the external command.
- shortener READMEWhich you should.
- masl.pl The Perl script that calls the remote service and shrinks / lengthens a given URL. The vim function requires this script be in the path and named masl.pl
All my code in this bundle is released under the GPL
This script makes no attempt to determine what is a valid URL. If you’re smart enough to be using vim then you are smart enough to only run this on actual URL's. If this is an issue let me know and i’ll add some URL scanning to masl.pl to look for valid URL and return a warning.
The WWW::Shorten Perl module this code uses was written by Dave Cross, Perl guru and all round decent chap. You can find his WWW::Shortenm odule on CPAN
Please note, Dave didn’t write the code included in this little bundle so DO NOT hassle him with support requests, bug reports or love letters. Especially not the love letters, I want those. Unless you’re male, old or named Keith.