Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created March 4, 2021 23:16
Show Gist options
  • Save OdatNurd/db3541e64a3d0a457e543aa9d98c6838 to your computer and use it in GitHub Desktop.
Save OdatNurd/db3541e64a3d0a457e543aa9d98c6838 to your computer and use it in GitHub Desktop.
Closing a Terminus view and pane at the same time
import sublime
import sublime_plugin
class CloseAndDestroyCommand(sublime_plugin.WindowCommand):
"""
Implement a close_and_destroy command which will close the current
terminus view and then destroy the origami pane that it's inside of.
"""
def run(self):
self.window.run_command("terminus_close");
self.window.run_command("destroy_pane", {"direction": "self"})
[
// If you use the Chain of Command package, use this binding to chain
// the two required commands together.
{ "keys": ["ctrl+shift+h"], "command": "chain",
"args": {
"commands": [
["terminus_close"],
["destroy_pane", {"direction": "self"}]
]
},
"context": [{ "key": "terminus_view"}]
},
// If you use the Multicommand package, use this binding to chain the
// two required commands together.
{ "keys": ["ctrl+shift+h"], "command": "multicommand",
"args": {
"commands": [
{
"command": "terminus_close"
},
{
"command": "destroy_pane",
"args": {"direction": "self"}
}
]
},
"context": [{ "key": "terminus_view"}]
},
// If you don't use either package, then you can install the sample plugin
// and use a binding like this to do the same thing.
{ "keys": ["ctrl+shift+h"], "command": "close_and_destroy",
"context": [{ "key": "terminus_view"}]
},
]
@OdatNurd
Copy link
Author

OdatNurd commented Mar 4, 2021

A question from the Make any build system interactive with Terminus video asked for a way to both close a Terminus tab and also use the Origami command to destroy the pane in one shot.

The above are three examples of this, one that uses the Chain of Command package, one that uses the Multicommand package, and one that uses just a simple plugin.

If you use either of the two packages, use the appropriate key bindings; alternately you can instead use the plugin which does the same thing, but in a more targetted way. In any of the cases choose the appropriate key binding and add it to your user key bindings, which have a name similar to the above but where PLATFORM will be one of Windows, Linux or OSX.

If you're unfamiliar with them, both packages are outlined in Chain of Command and Multicommand and Origami is outlined in the Origami video.

If you choose go to the plugin route and you're not sure how to set it up, see How to use plugins.

@boredabi
Copy link

boredabi commented Mar 5, 2021

thank you so much, it really helps.

@boredabi
Copy link

boredabi commented Mar 6, 2021

recently encountered an error with the chain of command package code, it only closes a single pane and pushes the rest to the right. is there any way to only close one at a time?

@OdatNurd
Copy link
Author

OdatNurd commented Mar 6, 2021

Hmm, I'm not entirely sure what you mean. If you destroy the current pane (which the above does) you're effectively changing the layout of the window; for example if you had it as two columns, that will make it one.

It sounds like your issue is that if you had a window split in half and with multiple tabs open in both, triggering the pane to destroy moves the files to a different group, or something along those lines?

@cbonet
Copy link

cbonet commented Feb 23, 2022

Thanks! This was really helpful. May I ask what "context": [{ "key": "terminus_view"}] does?

@OdatNurd
Copy link
Author

The context makes the key binding only available in cases where the condition(s) specified in the the context have to evaluate to true in order for the key binding to be active; otherwise it's effectively ignored. In this particular case it makes sure that the key only has an effect when the thing that currently has the input focus is a Terminus view,.

This video covers the topic in more depth: https://youtu.be/Vl8WLZ3EmOc

@cbonet
Copy link

cbonet commented Feb 23, 2022

Thanks for the explanation! I will watch the video

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