The Terminus package includes its own integrated terminus_send_string
command that can be used to send a string to a Terminus view, though it can only transmit predefined strings unless you were to use a bit of glue plugin code.
The idea of the command is demonstrated in the README as the last item before the FAQ
at the bottom, showing an example of simple plugin code to trigger the command:
window.run_command(
"terminus_send_string",
{
"string": "ls\n",
"tag": "<YOUR_TAG>" # ignore this or set it to None to send text to the first terminal found
"visible_only": False # send to visible panels only, default is `False`. Only relevent when `tag` is None
}
)
So if you only ever want to trigger static text being sent, you could bind the terminus_send_string
command to a key to do it. That doesn’t help you with the selection angle, though.
For that, you need a bit of plugin code that pulls the selection out of the buffer first. A simple example of that is the following (see this video if you’re not sure how to use plugins):
import sublime
import sublime_plugin
class SendSelectionToTerminusCommand(sublime_plugin.TextCommand):
"""
Extract the contents of the first selection and send it to Terminus.
"""
def run(self, edit, tag=None, visible_only=False):
self.view.window().run_command("terminus_send_string", {
"string": self.view.substr(self.view.sel()[0]),
"tag": tag,
"visible_only": visible_only
})
This grabs whatever text is currently selected in the buffer (only the first selection, assuming you have more than one selection) and then uses the above command to send it to Terminus.
The command is set up to take the same tag
and visible_only
arguments as the Terminus command takes, and just passes them through.
To use it, you want a keybinding something like the following (though of course the key you use is up to you):
{
"keys": ["ctrl+enter"],
"command": "send_selection_to_terminus",
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": false },
{ "key": "num_selections", "operator": "equal", "operand": 1 },
],
},
You can add in an "args"
key if you want to specify the other two arguments, which determine which Terminus view the output should be sent to (if you have more than one) or if it should only happen when the Terminus view is already visible.
As outlined above, it sends the selection directly, so in order to select code to execute you need to select the whole line (i.e. the cursor needs to be at the start of the following line); otherwise it just sends the text and you have to manually press enter.
The plugin could be modified to append a newline if there isn’t one if you always want this to execute the selection even if it’s not a full line though.