Skip to content

Instantly share code, notes, and snippets.

@DisposaBoy
Created January 22, 2014 08:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DisposaBoy/8555297 to your computer and use it in GitHub Desktop.
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 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.")
@dmitshur
Copy link

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.

@dmitshur
Copy link

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