sublimetextsublimetext4

Override next character when the same is typed in Sublime text 4


Is there a way how to set in key-binding file new rule, that when I type character which is already right after the cursor, it will be overridden.

To clarify what I would like to have:

if (ABC === XYZ) { // when I place cursor right after "XYZ" and type ")" mark, it will not be inserted and cursor jump right after the ")" which is already there
  echo "equals"; // when I place cursor right ater "equals" and type ";" mark, it will not be inserted and cursor jump right after the semicolon which is already there. When I type it again then, it will be inserted as usual
}

Solution

  • Yes, this is possible - when you press the key you want to skip, check if the character immediately to the right of the caret is the same as the key you pressed, and move over it if so. Here is the concrete bindings for semi-colon and closing paren: (unforunately it involves a bit of repetition because the context can't take a parameter based on which key was pressed without writing some plugin code)

        { "keys": [";"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
          [
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^;", "match_all": true },
          ]
        },
        { "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
          [
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
          ]
        },