Fork me on GitHub

article

curl’ing a URL in TextMate

August 18, 2011 | TextMate | 1 Comment

Ok, sorry, I can’t let all the BBEdit users think that they’ve discovered something new ;)

In case you’ve noticed the amazement lately on blogs and twitter where BBEdit users have discovered a way to open a curl’d url in BBEdit to view its source you may be wondering “how do I do this in TextMate”?

Well, its a little more code, but its also a little more powerful.

  1.  #!/usr/bin/env ruby -wKU
  2.  require ENV['TM_SUPPORT_PATH'] + '/lib/io.rb'
  3.  require ENV['TM_SUPPORT_PATH'] + '/lib/current_word'
  4.  require 'open3'
  5.  require 'cgi'
  6.  theUri = Word.current_word('a-zA-Z0-9#\-_\.:;%/?&=@!$^\*\+', :both)
  7.  if theUri.include? '@'
  8.   auth, uri = theUri.split('@')
  9.   theCommand = "curl -s -u #{auth} \"#{uri}\""
  10.  else
  11.   theCommand = "curl -s \"#{theUri}\""
  12.  end
  13.  stdin, stdout, stderr = Open3.popen3(theCommand)
  14.  TextMate::IO.exhaust(:out => stdout, :err => stderr) do |data|
  15.   puts data.rstrip
  16.  end

So, add this as a command in your bundle and tell it to open a new document when its done. Assign it to a keyboard command (if you haven’t already assigned other items to every other keyboard combination under the sun like I have). From here you can simply highlight a URL in a document, trigger the command, and get the source of the url in a new document.

On top of that, if the url contains credentials a-la “user:password@http://foo.com” it’ll use the “user:password” portion to authenticate you against simple Apache 401 authentication.

I’ve been thinking that it would be nice to have this also check the clipboard if there’s nothing selected. But I haven’t gotten around to trying that yet.

This and a few more helpful commands can be found in my General TextMate Bundle on Github (also available through GetBundles).

Enjoy!