Home > database >  In VS Code, is it possible to execute keybinds based on the context of the line, or characters
In VS Code, is it possible to execute keybinds based on the context of the line, or characters

Time:12-08

For example, if I'm writing something like this:

vector<int> v(10);

v[2<cursor is here>]

Is there some kind of when clause to add to custom keybinds, i.e., something along these lines:

{
    "key": "tab",
    "command": "cursorRight",
    "when": "textInputFocus && cursorNextChar == ']'"
}

And if nothing like that is built-in, what's a straightforward way to go about creating an extension that provides keybind extention customizability that executes commands based on the context of the line/characters around the cursor?

I tried using !atEndofWord but that breaks a lot of things like autocomplete and having tabs work as expected when not inside brackets.

CodePudding user response:

The extension Extra Context (v0.4.0) fills the context variable extraContext:editorCursorNextChar with the character after the active position (cursor) of the first selection. The context variable is the empty string when the position is at the end of a line.

For your example set this key binding:

  {
    "key": "tab",
    "command": "cursorRight",
    "when": "textInputFocus && extraContext:editorCursorNextChar == ']'"
  }
  • Related