Conditional keyboard shortcuts? Similar to when-clause contexts in VS Code

VS Code has contexts:

Visual Studio Code sets various context keys and specific values depending on what elements are visible and active in the VS Code UI. These contexts can be used to selectively enable or disable extension commands and UI elements, such as menus and views.

These contexts can be used by extension creators or end-users.

Personally I use them to create keyboard shortcuts, here are two examples:

{
    "key": "ctrl+shift+alt+cmd+t",
    "command": "workbench.action.terminal.new",
    "when": "!terminalIsOpen"
}

The contexts can also be combined:

{
    "key": "ctrl+down",
    "command": "search.action.focusSearchList",
    "when": "hasSearchResult && searchViewletFocus"
}

I think this is a very useful feature to create powerful keyboard shortcuts that are focused.


I dove a bit further into the topic of keyboard shortcuts in Obsidian and it seems like it doesn’t currently expose any keybindings.json or similar file for keyboard shortcuts. Probably that feature would need to be built first.

5 Likes

That’s the feature that I miss too.

2 Likes

yea that’s how I’m doing my console.logs in js and others using snippets like

  "print wrap copy plus": {
    "prefix": "print_wrap_copy_plus",
    "body": ["console.log(\"${CLIPBOARD}\", ${CLIPBOARD})"],
    "description": "debug print selected"
  }

and a keybinding
{
“key”: “ctrl+shift+v”,
“command”: “editor.action.insertSnippet”,
“args”: {
“name”: “print wrap copy plus”
},
“when”: “editorTextFocus”
},
and a shortcut as


result of the clipboard content pasted as console.log(“content”, content)
the same on selection… terrific

but let’s get back to obsidian.
for instance before you could do ctrl + enter to toggle bullet in the editor
now if you add it, it’s telling that’s in conflict. but the other ctrl enter should be working anyway because it’s on tabs. so obviously context is IMPORTANT

Keyboard shortcuts for context-dependent actions.

E.g.
I can’t assign the same shortcut to rename a file and a heading.
Ideally the action should compose nicely within the context.
However, having a focus context (Files vs Editor) should work and it should be as simple as changing the shortcut priority (sorting) based on context.

1 Like

Need this badly! :grimacing: