07.14.06

Are You Feeling Lucky?

Posted in TextMate at 11:06 pm by Haris

You know the feeling, you are typing along in TextMate, creating a new blog post or whatnot, and you need to link to some web page, for instance the webpage for GeekTool. So you move over to Camino or Safari or whatnot, and do a “feeling lucky” search for it, or perhaps a google search and of course it is the top link.

But why would you have to leave TextMate to do all that? Well, you don’t have to anymore:

Here’s a tiny little command I call, quite simply, “Feeling Lucky”, which will do a “lucky” search for its input and will output the corresponding link:

#!/usr/bin/env ruby
word =STDIN.read
require 'net/http'
response = Net::HTTP.get_response(URI.parse(URI.escape("http://www.google.com/search?q=#{word}&btnI=I'm+Feeling+Lucky")))
print response.to_hash['location']

If you want, you can turn the current selection directly into a Markdown link by replacing the last print command with:

print "[#{word}](#{response.to_hash['location']})"

In fact I used this modified command four times in this post, to generate the four links it contains.

What do you think? Should this be added to the Markdown bundle?

Update: It has now been added to the Markdown bundle. Also Damien Tanner, inspired by this, wrote a command that gets hold of the title of a page associated with a url.

Update 2: People are picking this up and messing with it in many lovely ways.

Brought to you by the LGFT (Little Gems For TextMate) project.

Later

13 Comments »

  1. The harvest on 15/07/06 said,

    July 15, 2006 at 3:52 pm

    […] Are You Feeling Lucky? - Easily get that url from within Textmate. […]

  2. Grab title of url - Textmate snipped said,

    July 15, 2006 at 5:01 pm

    […] After using Haris’s Are You Feeling Lucky? snippet for a few minutes I realised being able to do it the other way and get the title for an url, would be pretty useful too. Thus I output: […]

  3. Kevin Marsh said,

    July 16, 2006 at 10:15 am

    Wicked clever!

  4. Ben Jackson said,

    July 16, 2006 at 11:20 am

    Nice one! You should check it into SVN.

  5. Haris said,

    July 16, 2006 at 1:28 pm

    Ben,

    It’s already there ;) In the Markdown bundle. Added last night.

  6. Dr. Drang said,

    July 26, 2006 at 7:25 pm

    I took your clean, elegant solution and dirtied it up to make reference-style Markdown links.

    http://www.leancrew.com/all-this/2006/07/lucky_linking.html

    Forgive me. Engineers have an overwhelming compulsion to screw up the work of mathematicians.

  7. Haris said,

    July 26, 2006 at 9:05 pm

    Forgive me. Engineers have an overwhelming compulsion to screw up the work of mathematicians.

    That’s ok, our work needs screwing up ;)

  8. manu3000 said,

    July 27, 2006 at 3:01 am

    I get this when i execute the command in TextMate
    Any idea ? Thanks…

    /tmp/temptextmate.Pel6CY:6: syntax error
    require ‘net/http’response = Net::HTTP.get
    response(URI.parse(URI.escape(”http://www.google.com/search?q=#{word}&btnI=I’m+Feeling+Lucky”)))

  9. Haris said,

    July 27, 2006 at 6:33 am

    manu3000

    what version of Ruby do you have? Does this happen regardless of the selected word? Is this for the command that is now part of Markdown, or did you copy and paste from here?
    Make sure you have the most recent version of the bundle from svn. Email the list if you still have problems.

  10. manu3000 said,

    July 27, 2006 at 8:20 am

    I updated my Bundles and it now works fine
    Thanks!

  11. Brett Terpstra said,

    September 18, 2006 at 5:35 pm

    I’ve just picked up TextMate a couple days ago and your blog has been a big help to me. Thanks!

    Just wanted to let you know that I’ve taken “Lucky Linking” and it’s sidekick (get title) and combined them to output a link with the title of the page as the title of the link and the original phrase as the text of the link. It’s working quite well…

  12. Circle Six Blog » Blog Archive » Advanced Tools for Wordpress: Part 2 said,

    September 19, 2006 at 1:13 pm

    […] The HTML bundle comes with a command to automatically search Google and insert a link to the first result that comes up (”lucky linking“). It’s really cool. I modified it to add a title attribute based on the title tag in the head section of the returned page. Then I wanted to do the same thing for Wikipedia, but it wouldn’t work. Wikipedia required a User-Agent string. So I had to seriously modify the code. And then there was a far better chance on Wikipedia that a page wouldn’t be found, so I needed some error checking. Here’s the resulting code: […]

  13. Circle Six Blog » Blog Archive » More TextMate and Blogging said,

    September 29, 2006 at 6:06 am

    […] The bundle also includes an enhanced Google “lucky linking” command. Really, the biggest addition is that it opens the linked page and pulls out a title automatically, inserting it as a title attribute in the link. There is also a new version of the “Get Title of URL” command. […]

Leave a Comment