Sublime Forum

Sublime Text 2 Open in Browser

#1

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.

0 Likes

#2

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]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))[/code]

James

0 Likes

#3

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.

0 Likes

#4

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):

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)
0 Likes

#5

I wonder what mechanism Python uses for opening browser. It opens IE here even if my default browser is Opera.

0 Likes

#6

I believe Python uses your default browser.

This will open the current file in your browser.

[code]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())[/code]
I did find that attempting to open a *.py file will try and run Python itself.

0 Likes

Sublime 2 test in browser
#7

Works great! Thanks to all.
Phil

0 Likes

#8

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

>>> 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.

0 Likes

#9

Try with open_browser

0 Likes

#10

view.run_command(‘open_browser’)

0 Likes

#11

Ah that worked, thank you.

So the code posted by jbrooksuk is not needed?

0 Likes

#12

[quote=“neotoxic”]Ah that worked, thank you.

So the code posted by jbrooksuk is not needed?[/quote]

Yes it is.
open_browser is not an internal command so you need the plugin for it.

0 Likes

#13

Ah cool, I was trying to understand how the plugin was working. I can understand that the console command that is being run buy this code is called ‘open_browser’

view.run_command('open_browser')

But I was expecting to see the name of that command name ‘open_browser’ in the plugin code somewhere.

[code]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())[/code]
How dose open_browser relate to the plugin class OpenBrowserCommand

0 Likes

#14

More info here:

sublimetext.info/docs/en/extensi … mand-names

0 Likes

#15

That’s great, thank you for taking the time to post this link, I shall have a read.

0 Likes

#16

I have pretty good luck using the below. Note: I use this on windows. This will open all files in sublime into firefox or you can change out your preferred browser.

import sublime, sublime_plugin
import subprocess

class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      subprocess.call([r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', '-new-tab', self.view.file_name()])
0 Likes

#17

I apologize, i’m a super noob, to create this plugin in ST2 I would go to Tools > New Plugin, then I would select all and paste this code:

[code]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())[/code]

Then I would save it under the default User folder it opens when I press ctrl-S.
After that, how would I launch it with a keyboard shortcut, again, i apologize for my noobness.

0 Likes

#18

@nicktheandroid,

To assign any action to a key binding, look in your “Preferences > Default Key Bindings” or “Preferences > User Key Bindings.” This is where all of your key bindings are. For open in browser, add this to your “user key bindings”:

{ "keys": "super+shift+b"], "command": "open_browser" }

btw: you can very quickly get to your key binding files using the command palette

0 Likes

#19

Trying to get this to work. I’ve added other keybindings but this one isn’t opening a browser.
Is there some other setting I’m missing? I’m using ST2 2181

0 Likes

#20

[code]import sublime, sublime_plugin
import subprocess

class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self,edit):
subprocess.call([r’C:\Program Files (x86)\Mozilla Firefox\firefox.exe’, ‘-new-tab’, self.view.file_name()])[/code]

Im using this code mentioned previous, but it will open the file in the browser with file:/// is there a way to change it to localhost/?

0 Likes