├── Side Bar.sublime-menu ├── Default.sublime-commands ├── NonTextFiles.py └── Readme.md /Side Bar.sublime-menu: -------------------------------------------------------------------------------- 1 | [ 2 | {"id": "non_text_files", "caption": "Do Nothing", "command": "non_text_files_right_click"} 3 | ] -------------------------------------------------------------------------------- /Default.sublime-commands: -------------------------------------------------------------------------------- 1 | [ 2 | 3 | { 4 | "caption": "File: Open this file with an external application", 5 | "command": "open_externally", 6 | "args": {"then_close": false} 7 | } 8 | 9 | ] -------------------------------------------------------------------------------- /NonTextFiles.py: -------------------------------------------------------------------------------- 1 | import sublime 2 | import sublime_plugin 3 | import subprocess 4 | import os 5 | from fnmatch import fnmatch 6 | 7 | 8 | def open_file(filepath): 9 | if sublime.platform() == "osx": 10 | subprocess.Popen(('open', filepath)) 11 | elif sublime.platform() == "windows": 12 | os.startfile(filepath) 13 | elif sublime.platform() == "linux": 14 | subprocess.Popen(('xdg-open', filepath)) 15 | 16 | 17 | class OpenExternallyCommand(sublime_plugin.WindowCommand): 18 | 19 | def run(self, path=None, then_close=False): 20 | view = self.window.active_view() 21 | path = path or view.file_name() 22 | if path: 23 | open_file(path) 24 | if then_close: 25 | if hasattr(view, "close"): 26 | view.close() 27 | else: 28 | self.window.run_command("close") 29 | else: 30 | view.set_status("NTFiles", "Cannot open file with external application") 31 | sublime.set_timeout(lambda: view.erase_status("NTFiles"), 10000) 32 | 33 | def is_enabled(self): 34 | if self.window.active_view(): 35 | return self.window.active_view().file_name() is not None 36 | else: 37 | return False 38 | 39 | class NonTextFilesRightClickCommand(sublime_plugin.WindowCommand): 40 | 41 | def run(self, **kw): 42 | pass 43 | 44 | def is_visible(self, **kw): 45 | OpenFileExternally.bypass = True 46 | return False 47 | 48 | 49 | def enableOpenFileExternally(): 50 | OpenFileExternally.bypass = False 51 | 52 | 53 | class OpenFileExternally(sublime_plugin.EventListener): 54 | 55 | bypass = False 56 | 57 | def on_load(self, view): 58 | # I reset the bypass flag with a timeout to fail gracefully 59 | # if ST calls the `is_visible` method after the `on_load` event. 60 | sublime.set_timeout(enableOpenFileExternally, 500) 61 | if OpenFileExternally.bypass: 62 | return 63 | path = view.file_name() 64 | if not path: 65 | return 66 | for gpat in view.settings().get("open_externally_patterns", []): 67 | if fnmatch(path, gpat): 68 | open_file(path) 69 | break 70 | 71 | 72 | class PreventBinPreview(sublime_plugin.EventListener): 73 | last_path = None 74 | 75 | def reset_path(self): 76 | self.last_path = None 77 | 78 | def on_load(self, view): 79 | win = view.window() or sublime.active_window() 80 | if win and view.settings().get("prevent_bin_preview", True): 81 | path = view.file_name() 82 | if path and self.last_path != path: 83 | for gpat in view.settings().get("binary_file_patterns", []): 84 | if fnmatch(path, gpat): 85 | if hasattr(view, "close"): 86 | view.close() 87 | else: 88 | sublime.set_timeout(lambda: win.run_command("close"), 0) 89 | break 90 | self.last_path = path 91 | sublime.set_timeout(lambda: self.reset_path(), 500) 92 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Non-Text-Files Sublime Text plugin 2 | 3 | Sometimes it would be more useful if Sublime Text launched the default application for certain file types instead of displaying it in the editor. 4 | For example, if you open a PDF from the Sublime Text sidebar, the incomprehensible stream of compressed data is most often not what you intended to view. Instead you would like to view the document using your default viewer, be it Acrobat or Evince or any other. 5 | 6 | This plugin offers settings and commands to control when Sublime should open certain files with external applications. 7 | 8 | There is an option to turn off previews for binary files and options to open files with their default external applications. 9 | 10 | If you like this plugin and would like to support its development please consider donating through a [paypal donation][paypal]. 11 | 12 | ## Changelog 13 | 14 | **v1.3** 15 | 16 | + "Open Externally" should now handle right click on sidebar correctly (fixes #2) 17 | 18 | **v1.2** 19 | 20 | + multiple matching patterns do not trigger open multiple times 21 | + double click behaviour is now more stable and predictable 22 | + preview closing is more reliable 23 | 24 | 25 | ## Installation 26 | 27 | 1. Install [Sublime Text](http://www.sublimetext.com/) 28 | 29 | 2. Install the plugin either: 30 | - with **Package Control**: see , or 31 | - **manually**: by cloning this repository in your Sublime Text Package directory 32 | 33 | 3. Customise the `open_externally_patterns` setting in your preferences 34 | (`Preferences > Settings - User`) 35 | 36 | ## Features and settings 37 | 38 | ### Prevent Binary File Preview 39 | 40 | When the setting `prevent_bin_preview` is set to `true` (default), clicking on a file matching any of the `binary_file_patterns` would not open the file. 41 | Double click will open it normally: this is useful when using plugins like [Hex​Viewer] or [Zip Browser]. 42 | 43 | The `prevent_bin_preview` settings can be set globally in the `User/Preferences.sublime-settings` file or locally to a project. 44 | 45 | ### Open files with external applications 46 | 47 | Sometimes it would be more useful if Sublime Text launched the default application for certain file types instead of displaying it in the editor. 48 | This plugin allows you to do that via a special option: the files matching any of the `open_externally_patterns` will be opened with the default application as configured in your OS instead of using Sublime. You can set it globally in the `User/Preferences.sublime-settings` file or locally to a project. 49 | This setting follows the same syntax of the `binary_file_patterns` setting: it is just a list of [glob patterns](https://en.wikipedia.org/wiki/Glob_%28programming%29). An example: 50 | 51 | "open_externally_patterns": [ 52 | "*.jpg", 53 | "*.jpeg", 54 | "*.png", 55 | "*.gif", 56 | "*.zip", 57 | "*.pdf" 58 | ] 59 | 60 | The plugin also offers a window command `open_externally` that opens a file with the default application. It takes two optional arguments: 61 | 62 | - `path` is the path of the file to be opened; if empty the file of the current view will be opened; 63 | - `then_close` if true will close the view after opening the file. 64 | 65 | You can bind this command to a shortcut by adding the following to your keymap: 66 | 67 | ```json 68 | { 69 | "keys": ["super+enter"], "command": "open_externally", 70 | "args": {"then_close": false} 71 | } 72 | ``` 73 | 74 | 75 | 76 | 77 | [Hex​Viewer]: 78 | [Zip Browser]: 79 | 80 | [paypal]: 81 | --------------------------------------------------------------------------------