Today: A filter for URI encoding / decoding
The script introduced in this post will read input from stdin, encode / decode URI’s and URI components and spit the result to stdout.
Say you are working in Vim (which makes sense) and need to encode a URI component that in its unencoded form looks like so (it’s the bit after question mark):
<a href="http://yourdomain.com/click.cgi?url=http://someotherdomain.com/whatever/somefile.html">Your linktext here</a>
If you run a site with standard-compliant HTML (which you should) then the above link should show up in your source like so:
<a href="http://yourdomain.com/click.cgi?url=http%3A%2F%2Fsomeotherdomain.com%2Fwhatever%2Fsomefile.html">Your linktext here</a>
Maybe you belong to those maniacs who speak, write and read hex and just type stuff like that as you go. Well congratulations, you won’t need this. If, however, you belong to the rest of us who would rather have a script do such conversions for you, then the following can help:
Park this somewhere in your path – mine sits in ~/bin/ and is called urler. Zero points for imaginative naming, as usual.
The smallest piece of information Vim can send to a filter is a line. Meaning, the bit you send to the script needs to sit on a line of its own.
This is how it works:
Write or paste your URI (component) onto a line of its own. Select with
v$h to make sure that you don’t send any trailing whitespace to the script. Type : and the status line will look like
:'<,'> now type
,!urler c <Enter>. Your URI component will be replaced by its encoded version.
You can of course do the same with query strings to encode / decode those pesky ampersands that just about every CMS uses as arg separators.