0

I'd like cmd+1 to alternate between reveal in sidebar (if closed) and if sidebar is open, close it.

if closed: { "keys": ["super+1"], "command": "reveal_in_side_bar"}

if open: { "keys": ["super+1"], "command": "toggle_side_bar" }

I don't know how to do the if part . Thanks

Adam
  • 3,415
  • 4
  • 30
  • 49

1 Answers1

2

As far as I know, there is no built-in key binding context that can be used to tell whether the sidebar is open or closed. But this is something that can easily be done using the Python API, specifically with window.is_sidebar_visible() and it is also possible to create custom keybinding contexts.

From the Tools menu, navigate to Developer > New Plugin. Then replace the contents of the view with:

import sublime, sublime_plugin

class SidebarContextListener(sublime_plugin.EventListener):
    def on_query_context(self, view, key, operator, operand, match_all):
        if key != 'sidebar_visible' or not (operand in ('reveal', 'toggle')):
            return None
        visible = view.window().is_sidebar_visible()
        if operand == 'toggle' and visible:
            return True
        if operand == 'reveal' and not visible:
            return True
        return None

and save it, in the folder ST suggests (Packages/User) as something like sidebar_context.py - the extension is important, the name isn't.

Now, we can use it in your keybindings, like:

{ "keys": ["super+1"], "command": "toggle_side_bar", "context":
    [
        { "key": "sidebar_visible", "operand": "toggle" },
    ],
},

{ "keys": ["super+1"], "command": "reveal_in_side_bar", "context":
    [
        { "key": "sidebar_visible", "operand": "reveal" },
    ],
},
Adam
  • 3,415
  • 4
  • 30
  • 49
Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • disclaimer: I wrote this on mobile, so please forgive me if there is a syntax error or something and it doesn't work - also, feel free to edit the answer and fix it up :) – Keith Hall May 12 '18 at 10:25
  • Thank you for this - it looks really good, but I need to dive to python docs and to sublime docs before I can fix this up first. – Adam May 12 '18 at 15:53
  • 2
    With the (slight) edit I made above it seems to work OK for me. Note that the command for toggling the sidebar is not what you thought. Does the code as above work for you now? – OdatNurd May 12 '18 at 17:21