Removing title attributes inserted by TextMate’s Hyperlink Helper bundle

As I have mentioned in several previous posts – Don’t duplicate link text in the title attribute is one – automatically adding a title attribute to every link you create is not particularly good practice. Unfortunately one very useful command in TextMate does exactly that – the Wrap Word / Selection as Link command from the Hyperlink Helper bundle.

What the Wrap Word / Selection as Link command (Shift + Ctrl + L) does is wrap the currently selected text (or if no text is selected, the word the cursor is in or next to) in a link to the URL in the clipboard. This is very useful when inserting links in blog posts for instance. The downside is that Wrap Word / Selection as Link will also insert a title attribute with the title of the page the link points to. In my experience that is almost always unnecessary, so I’ve removed the offending title attributes after creating links in TextMate a gazillion times.

Well, I finally realised that it doesn’t have to be this way and that it’s easy to edit the Hyperlink Helper bundle to remove the annoying title attributes completely. In case there are others who also want to do this, here’s how:

  1. Open the Bundle Editor (press Ctrl + Opt + Cmd + B or go to Bundles > Bundle Editor > Show Bundle Editor )
  2. Open the Wrap Word / Selection as Link command in the Hyperlink Helper bundle
  3. Comment out the code that inserts the title attribute by changing this:

    if url =~ /^http:\/\// and url != 'http://example.com/'
      eval 'title = fp.read.match(/<title>([^<>]*)<\/title>/i).to_a[1].strip rescue nil' if fp = open(url) rescue nil
    end

    Into this:

    #if url =~ /^http:\/\// and url != 'http://example.com/'
    #  eval 'title = fp.read.match(/<title>([^<>]*)<\/title>/i).to_a[1].strip rescue nil' if fp = open(url) rescue nil
    #end
  4. Close the Bundle Editor

To verify that your changes work, copy a URL to the clipboard, select some text in TextMate and hit Shift + Ctrl + L. The inserted link should not have a title attribute. On the rare occasions when a title attribute is appropriate, you can obviously still add it manually.

An additional benefit this little tweak has is that it speeds up the command since TextMate no longer has to download the entire document you link to (to get the document title) before inserting the link.

Posted on December 7, 2010 in Coding

Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.