Sublime Text 2 keyboard shortcut to open file in specified browser (e.g. Chrome)

后端 未结 14 2153
野性不改
野性不改 2020-12-02 04:31

I do web development and am trying out Sublime Text 2. Is there a keyboard shortcut to open the current file in specified browser (e.g. Chrome)?

Any help to get set

相关标签:
14条回答
  • 2020-12-02 05:01

    Here is another solution if you want to include different browsers in on file. If you and Mac user, from sublime menu go to, Tools > New Plugin. Delete the generated code and past the following:

    import sublime, sublime_plugin
    import webbrowser
    
    
    class OpenBrowserCommand(sublime_plugin.TextCommand):
       def run(self,edit,keyPressed):
          url = self.view.file_name()
          if keyPressed == "1":
             navegator = webbrowser.get("open -a /Applications/Firefox.app %s")
          if keyPressed == "2":
             navegator = webbrowser.get("open -a /Applications/Google\ Chrome.app %s")
          if keyPressed == "3":
             navegator = webbrowser.get("open -a /Applications/Safari.app %s")
          navegator.open_new(url)
    

    Save. Then open up User Keybindings. (Tools > Command Palette > "User Key bindings"), and add this somewhere to the list:

    { "keys": ["alt+1"], "command": "open_browser", "args": {"keyPressed": "1"}},
    { "keys": ["alt+2"], "command": "open_browser", "args": {"keyPressed": "2"}},
    { "keys": ["alt+3"], "command": "open_browser", "args": {"keyPressed": "3"}}
    

    Now open any html file in Sublime and use one of the keybindings, which it would open that file in your favourite browser.

    0 讨论(0)
  • 2020-12-02 05:01

    egyamado's answer was really helpful! You can enhance it for your particular setup with something like this:

    import sublime, sublime_plugin
    import webbrowser
    
    class OpenBrowserCommand(sublime_plugin.TextCommand):
       def run(self, edit, keyPressed, localHost, pathToFiles):  
          for region in self.view.sel():  
             if not region.empty():  
                # Get the selected text  
                url = self.view.substr(region)  
                # prepend beginning of local host url 
                url = localHost + url  
             else:
                # prepend beginning of local host url 
                url = localHost + self.view.file_name()
                # replace local path to file
                url = url.replace(pathToFiles, "")
    
    
             if keyPressed == "1":
                navigator = webbrowser.get("open -a /Applications/Firefox.app %s")
             if keyPressed == "2":
                navigator = webbrowser.get("open -a /Applications/Google\ Chrome.app %s")
             if keyPressed == "3":
                navigator = webbrowser.get("open -a /Applications/Safari.app %s")
             navigator.open_new(url)
    

    And then in your keybindings:

    { "keys": ["alt+1"], "command": "open_browser", "args": {"keyPressed": "1", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}},
    { "keys": ["alt+2"], "command": "open_browser", "args": {"keyPressed": "2", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}},
    { "keys": ["alt+3"], "command": "open_browser", "args": {"keyPressed": "3", "localHost": "http://nbrown.smartdestinations.com", "pathToFiles":"/opt/local/apache2/htdocs"}}
    

    We store sample urls at the top of all our templates, so the first part allows you to highlight that sample URL and launch it in a browser. If no text is highlighted, it will simply use the file name. You can adjust the command calls in the keybindings to your localhost url and the system path to the documents you're working on.

    0 讨论(0)
提交回复
热议问题