Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created November 9, 2020 07:21
Show Gist options
  • Select an option

  • Save OdatNurd/b186d694b635a515d300d4e3f32e9338 to your computer and use it in GitHub Desktop.

Select an option

Save OdatNurd/b186d694b635a515d300d4e3f32e9338 to your computer and use it in GitHub Desktop.
Setting up a Python REPL in Sublime Text using Terminus
[
// Place the bindings in your user Key bindings for whatever platform you're
// on (also, change the keys as appropriate for your use).
// Send a newline to the Terminus Python REPL, if it's open
{ "keys": ["shift+ctrl+enter"], "command": "terminus_send_string",
"args": {"string": "\n", "tag": "python-repl"},
"context": [
{ "key": "terminus_tag.exists", "operand": "python-repl" },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
],
},
// Open a Python REPL using a Terminus buid if it's not already running.
{ "keys": ["ctrl+enter"], "command": "build",
"args": {
"build_system": "Packages/User/PythonREPL.sublime-build",
"variant": "",
},
"context": [
{ "key": "terminus_tag.notexists", "operand": "python-repl" },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
],
},
// Send the current selection or file content to the Terminus Python REPL,
// if it exists.
//
// You could also augment this by having different keys depending on if
// there is selected text or not, or constrain the number of selections,
// etc.
{ "keys": ["ctrl+enter"], "command": "send_selection_to_terminus",
"args": {"tag": "python-repl"},
"context": [
{ "key": "terminus_tag.exists", "operand": "python-repl" },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
],
},
]
{
"target": "terminus_open",
"title": "Python REPL",
"tag": "python-repl",
"auto_close": false,
"shell_cmd": "python -u -i \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"post_window_hooks": [
["carry_file_to_pane", {"direction": "right"}]
],
"env": {"PYTHONIOENCODING": "utf-8"},
}
import sublime
import sublime_plugin
def _terminus_view(window, tagName):
window = window or sublime.active_window()
for view in window.views():
if view.settings().get("terminus_view.tag") == tagName:
return view
return None
class SendSelectionToTerminusCommand(sublime_plugin.WindowCommand):
def run(self, tag=None, visible_only=False):
view = self.window.active_view()
if view == None:
return
if any(sel.empty() for sel in view.sel()):
spans = [sublime.Region(0, len(view))]
else:
spans = [sel for sel in view.sel() if not sel.empty()]
for span in spans:
self.window.run_command("terminus_send_string", {
"string": view.substr(span),
"tag": tag,
"visible_only": visible_only
})
class SampleREPLListener(sublime_plugin.EventListener):
def on_query_context(self, view, key, operator, operand, match_all):
if key == "terminus_tag.exists" or key == "terminus_tag.notexists":
view = _terminus_view(view.window(), operand)
return view != None if key == "terminus_tag.exists" else view == None
return None
@OdatNurd

OdatNurd commented Nov 9, 2020

Copy link
Copy Markdown
Author

These sample files show how to use the Terminus package for Sublime Text to create a custom Python REPL environment without having to also install SublimeREPL. This is demonstrated in my video on setting up a REPL using Terminus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment