skip to Main Content

The "when" clause in VSCode’s JSON keybindings editor is very useful, but I can’t seem to find a way to reverse it. For instance, if I want an action to only work when the Terminal view is not showing, I would want something like this:
"when": "not.terminalViewShowing"
or
"when.not": "terminalViewShowing"

Is something like this possible and if not, are there any plans to add it?

2

Answers


  1. If the when clause only contains one condition, then you can just invert that single condition with the ! operator. For the current list of operators in when-clauses, see the official docs: https://code.visualstudio.com/api/references/when-clause-contexts#conditional-operators.

    If the when clause contains multiple condition joined by logical operators…

    See this GitHub issue: Add support for parenthesis in "when" conditions #91473, which was added to VS Code’s March 2023 Milestone, and was closed as completed by the context keys: implement a new parser (and a scanner/lexer) for ‘when’ clauses #174471 pull request. You can read about the exact syntax and grammar there. Here’s a quote of its syntax and grammar in Extended Backus-Naur form:

    expression ::= or
    
    or ::= and { '||' and }*
    
    and ::= term { '&&' term }*
    
    term ::=
      | '!' (KEY | 'true' | 'false')
      | primary
    
    primary ::=
      | 'true'
      | 'false'
      | '(' expression ')'
      | KEY '=~' REGEX
      | KEY [ ('==' | '!=' | '<' | '<=' | '>' | '>=' | 'not' 'in' | 'in') value ]
    
    value ::=
      | 'true'
      | 'false'
      | 'in'     
      | KEY
      | SINGLE_QUOTED_STR
      | EMPTY_STR
    

    See also the announcement / discussion issue ticket for the new feature: Upcoming when clause context parser
    #175540
    .

    Fun note: Even before parenthesis support in when clauses got added, you could usually use De Morgan’s laws as a workaround to negate when-clauses.

    Login or Signup to reply.
  2. It is as simple as adding the ! before your terminalViewShowing.

    "when": "!terminalViewShowing"
    

    BYW, parentheses are not supported in when clauses, see Add support for parenthesis in "when" conditionshttps://github.com/microsoft/vscode/issues/91473 and https://github.com/microsoft/vscode/issues/147904.


    Update: A PR has just been merged that introduces a new parser for when clauses which does, finally, support parentheses, see context keys: implement a new parser (and a scanner/lexer) for ‘when’ clauses.

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