skip to Main Content

Im adding git.sync shortcut to my vscode
enter image description here

If I didnt commit , it shows
enter image description here

it means I need to add a proper when clause to my shortcut bindings:

{
    "key": "cmd+enter",
    "command": "git.sync"
    "when":""
  },

so I need to add a when clause, whats it?

search in google , and tried to use github copilot to get the when clause

2

Answers


  1. To add a "when" clause to your shortcut bindings for the "git.sync" command in Visual Studio Code, you can specify a condition that should be met for the shortcut to be active. In your case, you want the shortcut to be active only if there are uncommitted changes in your Git repository.

    You can use the following "when" clause to achieve this:

    "when": "gitUncommittedChanges"
    

    This clause checks if there are uncommitted changes in your Git repository before allowing the shortcut to trigger the "git.sync" command.

    So, your updated shortcut binding would look like this:

    {
        "key": "cmd+enter",
        "command": "git.sync",
        "when": "gitUncommittedChanges"
    }
    

    This ensures that the shortcut will only work when there are uncommitted changes in your Git repository.

    Login or Signup to reply.
  2. As far as I know, at the time of this writing (VS Code 1.85), there is no such context key you can use to do this. To substantiate:

    • You can see docs for available context keys in https://code.visualstudio.com/api/references/when-clause-contexts. I don’t see anything that sounds useful there when searching "scm" or "git".

    • Neither do I see anything undocumented by searching "when":.*scm with regex mode on in the defaultKeybindings.json pseudo-file.

    • Neither do I see anything undocumented by searching github.dev/microsoft/vscode. Searching (createKey|createScoped|createOverlayy|setContext|removeContext|getContext|createChildContext).*?(git|scm) with regex mode on,

      • There’s a git.state context key, but the only values listed for its type definition APIState in extensions/git/src/api/git.d.ts are 'initialized' and 'uninitialized'.
      • There’s also the gitRebaseInProgress and gitMergeInProgress context keys, but I don’t think those are sufficient to get what you want.
      • There’s also scmActiveResourceHasChanges and scmActiveResourceRepository, but those are only for the active resource (singular) and not the whole repository.
    • And looking at the existing issue tickets, I don’t see one already asking for this: https://github.com/microsoft/vscode/issues?q=is%3Aopen+is%3Aissue+label%3Acontext-keys.

    So I’d suggest that you raise a feature-request issue ticket asking for it. If you do, please comment here with a link to your issue ticket, or edit the link into this answer post.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search