Skip to content

Instantly share code, notes, and snippets.

@liberize
Last active January 2, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save liberize/8283362 to your computer and use it in GitHub Desktop.
Save liberize/8283362 to your computer and use it in GitHub Desktop.
sublime text plugin to open file in different browser.
[
{ "keys": ["alt+o", "alt+f"], "command": "open_in_browser_alt", "args": {"browser": "firefox"} },
{ "keys": ["alt+o", "alt+s"], "command": "open_in_browser_alt", "args": {"browser": "safari"} }
]
import sublime
import sublime_plugin
import subprocess
import tempfile
class OpenInBrowserAltCommand(sublime_plugin.ApplicationCommand):
def run(self, **kwargs):
browser = kwargs['browser']
browser_cmd_map = {
'safari': ['open', '-a', 'safari'],
'firefox': ['open', '-a', 'firefox']
}
if browser in browser_cmd_map:
cmd = browser_cmd_map[browser]
view = sublime.Window.active_view(sublime.active_window())
if view.file_name():
cmd.append(view.file_name())
subprocess.Popen(cmd)
else:
temp = tempfile.NamedTemporaryFile(delete=False)
content = view.substr(sublime.Region(0, view.size()))
temp.write(content.encode('utf-8'))
temp.flush()
cmd.append(temp.name)
subprocess.Popen(cmd)
temp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment