Sublime Forum

Input variable to key binding argument

#1

Please pardon what might be a naive question; I am quite new to Sublime Text. I am wondering if there is a way to write a custom key binding in which part of the argument is entered as text once the key binding is triggered. By way of an example, I have a key binding to open a Terminus shell, with arguments that activate the conda environment “myenv” and then launches IPython:

{ 
    "keys": ["ctrl+alt+p"], 
    "command": "terminus_open", 
    "args": {"shell_cmd": "conda activate myenv; ipython"}
}

Is there any way that I could specify which environment to activate upon triggering the keybinding, rather than having to hard-code it for a single pre-determined environment?

I’m imagining something where the shell_cmd argument would be something like "conda activate ${input}; ipython" and once I trigger the key binding, a text input field pops up in which I type the name of a conda environment, and that then becomes the environment in which IPython opens. Is something like that possible to do?

0 Likes

#2

A simple plugin should do the trick, I believe.

import sublime
import sublime_plugin

class OpenDesiredEnvCommand(sublime_plugin.WindowCommand):

	def run(self):
		self.window.show_input_panel("Enter env: ", "", self.on_env, None, None)

	def on_env(self, env_name):
		if env_name is not None:
			self.window.run_command("terminus_open", {
				"shell_cmd": "conda activate {}; ipython".format(env_name)
			})

This will ask you for the environment name by opening an input panel at the bottom & if it’s not empty, run the terminus_open command by passing the name of the environment name to shell_cmd. I don’t use anaconda anymore but do let me know if this works. You will have to save this as a .py file in the Users directory (to get to the Users directory, go to Preferences -> Browse Packages... from the main menu on the top). To make it work with your keybinding, define the key binding as:

{ "keys": ["ctrl+alt+p"], "command": "open_desired_env" }

in a .sublime-keymap file saved to the Users directory.

2 Likes

#3

Thank you so much for the suggestion, though it doesn’t seem to work. I tried what you said, and put the text from your code block into a file named open_desired_env.py in my Users directory, then defined the key binding. When I trigger the key binding, it does indeed open an input panel at the bottom, then I enter the name of one of my conda environments, but then it doesn’t seem to do anything after that.

If you have any further suggestions, I’m definitely game to try! Either way, this has already been a helpful learning experience for me. But it seems like it’s close to working, so perhaps some minor adjustment will bring it all the way there?

0 Likes

#4

My bad, the command should be terminus_open & not open_terminus (edited my previous answer). Can you check if this works ? Also the plugin I mentioned doesn’t check if the env_name is indeed a valid environment name. So you’ll have to type the exact environment name

1 Like

#5

Awesome, that totally works, thanks! Also, since you mentioned that it doesn’t check if the name is valid, I tried entering an invalid name (‘asdf’). It opens the Terminus panel, and returns

Could not find conda environment: asdf
You can list all discoverable environments with `conda info --envs`.

bash: ipython: command not found
process is terminated with return code 127.

Based on what you have taught me already, I think I might be able to figure out how to implement it so that it checks, and based on whether or not it’s valid, either opens Terminus or lists the valid environments (via conda info --envs). If I succeed, I will post back here.

0 Likes

#6

Definitely. Do post in this thread if you learn how to do that so that others can benefit from your discovery :slightly_smiling_face:
Of the top of my head, you should be able to do that by using the built in subprocess module (by executing conda info --envs, getting the output, parsing it, converting it into a list and then showing a show_quick_panel method defined on sublime.Window, whenever the env name is incorrect).

1 Like