Created
January 22, 2014 08:26
-
-
Save DisposaBoy/8555297 to your computer and use it in GitHub Desktop.
a sublime text plugin example to open the file path that's in the clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this plugin creates a command `open_from_clipboard` that opens the path that's currently in the clipboard | |
# save this file at Packages/User/openclipboard.py | |
# then add the follow key binding if you like via the menu Preferences > Key Bindings - User | |
# { "keys": ["ctrl+shift+o"], "command": "open_from_clipboard" } | |
import sublime | |
import sublime_plugin | |
class OpenFromClipboardCommand(sublime_plugin.WindowCommand): | |
def run(self): | |
fn = sublime.get_clipboard() | |
if fn: | |
sublime.active_window().open_file(fn) | |
else: | |
sublime.status_message("Nothing to open. The clipboard is empty.") |
I'm not good at Python and Sublime API, so this is probably far from idiomatic, but it works:
import sublime
import sublime_plugin
class OpenFromClipboardCommand(sublime_plugin.WindowCommand):
def run(self):
path = None
for r in sublime.active_window().active_view().sel():
s = sublime.active_window().active_view().substr(r)
if s != "":
path = s
break
if path == None:
path = sublime.get_clipboard()
if path != "":
sublime.active_window().open_file(path)
else:
sublime.status_message("Nothing to open. The selection and clipboard are empty.")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very useful, thank you!
How easy is it to extend this so that it first checks if there is text selected, and uses that as the path if there is? If no selection, it falls back to clipboard. If no clipboard, print that "Nothing to open." message.