Home Download Buy Blog Forum Support

Sublime Text 2 Open in Browser

Sublime Text 2 Open in Browser

Postby pgmagallanes on Mon May 30, 2011 4:37 pm

I am using Windows 64-bit Build 2059. In Sublime Text 1.4, ctrl-f9 was bound to the openInBrowser command.
In Sublime Text 2, I tried changing the command to open_in_browser but it did not work.
Is there another command that I should be using?
Thanks.
pgmagallanes
 
Posts: 6
Joined: Tue Mar 29, 2011 4:13 am

Re: Sublime Text 2 Open in Browser

Postby jbrooksuk on Tue May 31, 2011 9:54 am

Hi,

I wrote this plugin to open PHP.net for my PHP files. It runs a web browser with the URL that I need. Perhaps you could use it to make something similar for opening the current file in the browser?
Code: Select all
import sublime, sublime_plugin
import webbrowser

class OpenHelpCommand(sublime_plugin.TextCommand):

   def run(self,edit):
      helpLink = 'http://uk.php.net/manual-lookup.php?pattern={0}';
      for region in self.view.sel():
         if not region.empty():
            syntax = self.view.substr(region)
            webbrowser.open_new(helpLink.format(syntax))

James
jbrooksuk
 
Posts: 757
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Sublime Text 2 Open in Browser

Postby pgmagallanes on Thu Jun 02, 2011 4:06 am

James,

Thanks for the reply. I will have to brush up on my Python a little before I can use your code.
Where do I find a definition for "webbrowser?"

Thanks.
pgmagallanes
 
Posts: 6
Joined: Tue Mar 29, 2011 4:13 am

Re: Sublime Text 2 Open in Browser

Postby jbjornson on Fri Jun 03, 2011 11:50 am

Documentation can be found here: http://docs.python.org/library/webbrowser.html (found using a simple google search for "python webbrowser").

I'm not sure you really need to investigate much here, James did all the hard work. Maybe try something like this, which will open any urls you have selected (in your default web browser):
Code: Select all
import sublime, sublime_plugin
import webbrowser

class OpenHelpCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      for region in self.view.sel():
         if not region.empty():
            url = self.view.substr(region)
            webbrowser.open_new(url)
jbjornson
 
Posts: 345
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Sublime Text 2 Open in Browser

Postby rchl on Fri Jun 03, 2011 12:24 pm

I wonder what mechanism Python uses for opening browser. It opens IE here even if my default browser is Opera.
rchl
 
Posts: 147
Joined: Fri Mar 04, 2011 11:02 pm

Re: Sublime Text 2 Open in Browser

Postby jbrooksuk on Fri Jun 03, 2011 3:33 pm

I believe Python uses your default browser.

This will open the current file in your browser.
Code: Select all
import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      webbrowser.open_new(self.view.file_name())

I did find that attempting to open a *.py file will try and run Python itself.
jbrooksuk
 
Posts: 757
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Sublime Text 2 Open in Browser

Postby pgmagallanes on Fri Jun 03, 2011 9:12 pm

Works great! Thanks to all.
Phil
pgmagallanes
 
Posts: 6
Joined: Tue Mar 29, 2011 4:13 am

Re: Sublime Text 2 Open in Browser

Postby neotoxic on Wed Jul 20, 2011 9:07 am

Sorry for the noobie questions but I added the code to a new plugin and then tried to call OpenBrowserCommand from the console but it just says

Code: Select all
>>> OpenBrowserCommand
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'OpenBrowserCommand' is not defined


I am no doubt missing something obvious and fundamental.
neotoxic
 
Posts: 6
Joined: Sat Dec 26, 2009 10:56 am

Re: Sublime Text 2 Open in Browser

Postby jbjornson on Wed Jul 20, 2011 9:29 am

Try with open_browser
jbjornson
 
Posts: 345
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Sublime Text 2 Open in Browser

Postby bizoo on Wed Jul 20, 2011 9:35 am

jbjornson wrote:Try with open_browser

view.run_command('open_browser')
bizoo
 
Posts: 874
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Next

Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot], Yahoo [Bot] and 24 guests