Sublime Forum

Send selection to show_overlay command in keybind?

#1

Is it possible in someway to send selected text to show_overlay command in a keybind.

What i want, i have a code file with a include file in, ie "include “filepath/filename.ext”, that i want to open.
Now i just press ctrl+p, type in filename, or copy+paste, but would like to make another keybind, where i can select filename, press ctrl+o, and it does like if i had copied filename, pressed ctrl+p, and pasted it in.

That is something like this:

{ "keys": "ctrl+o"], "command": "show_overlay", "args": {"overlay": "goto", "text": "${SELECTED}"} },
1 Like

#2

I don’t think you can supply a selection to a command through key mapping. I believe you will need to roll your own plugin to get the selection and pass that value to the command. You would have to handle multiple cursors and such which the key mapping wouldn’t know how to do. You can try the following which should get you started although you will probably want to add some sort of validation to the plugin or context to the key mapping so its only accessible when you are over a valid include.

import sublime, sublime_plugin
class OpenIncludeCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        sel = view.sel()[0]
        word = view.substr(view.word(sel))

        self.window.run_command("show_overlay", {"overlay": "goto", "text": word, "show_files": True})

Then just call your new command through a new keyboard shortcut:


	{"keys": "ctrl+o"], "command": "open_include"}
]
1 Like

#3

Thank you, this works perfect for me!

Was using another solution, but this was overriding my clipboard each time, so this works much better :smile:

My own solution was this keybind, together with addon “Chain of command”:


	{
	  "keys": "ctrl+shift+o"], 
	  "command": "chain", 
	  "args": {
		"commands": 
			"copy"],
			"show_overlay",{"overlay": "goto"}],
			"paste"]
		]
	  },
	  "context": { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }]
	}
]
1 Like