Sublime Forum

How does the open_url command select the browser to use?

#1

I’m in the middle of creating a custom Main.sublime-menu file to add some entries for quickly getting the Unofficial docs and some of the parts of it I use most often and I’ve run into something weird here on my Linux machine regarding sublime opening links for me in that it’s not picking the correct browser.

I have Firefox and Chrome both installed, but Chrome is set to be the default browser.

As such, this opens the URL in chrome:

tmartin:dart:~> xdg-open "http://www.sublimetext.com/docs/3/api_reference.html"

Also, this code (executed from the Sublime console directly) opens the link in Chrome:

import webbrowser
webbrowser.open_new_tab ("http://www.sublimetext.com/docs/3/api_reference.html")

However, this code (also executed from the Sublime console directly) opens the link in Firefox instead:

window.run_command ("open_url", {"url": "http://www.sublimetext.com/docs/3/api_reference.html"})

I would have guessed that under the hood Sublime would be using the webbrowser module here, but I seems to act as if its not. It also doesn’t seem to respect the BROWSER environment variable, which is also set to point to Chrome.

I dropped this simple plugin and it fixes the problem, but I’m not sure why it’s necessary.

import sublime, sublime_plugin, webbrowser

class OpenUrlCommand(sublime_plugin.WindowCommand):
    def run(self, url):
        webbrowser.open_new_tab (url)

Is this a bug in sublime or am I just being an idiot here?

1 Like

On macOS, right click on a link and choose open, opens it in Firefox, not the default browser
#2

It is a mistery hidden within the sublimetext binary.

On Windows, it’s possible to use ProcessMonitor to discover how it does it. I guess there is some equivalent tool on Linux that could help?

0 Likes

#3

I would guess that the open_url command is depreciated, because it is not used in the Default package - the webbrowser.open_new_tab method is used instead, so I’m guessing that it is the preferred way to do this (even though open_url is used by the Help menu…)

1 Like

#4

I’d buy that as an explanation given the circumstances. If true, this seems like a bug (potentially of omission) since the open_context_url command from the default package uses the other method (as you mentioned above) instead of just invoking the open_url command once it’s pulled the URL from the view.

0 Likes

#5

btw, the “Open in Browser” context menu item behavior is currently being discussed here: https://github.com/SublimeTextIssues/Core/issues/1280

0 Likes