Ole
Ole

Reputation: 47038

Doing in line editing in VSCode using the Keyboard Only?

In VSCode we can create multiple cursors inside a single word or sentence by select alt or option (Mac) and then clicking where we want the additional cursors.

Is there a way to use the keyboard only to create the additional cursors (Instead of having to click)?

Upvotes: 0

Views: 115

Answers (1)

rioV8
rioV8

Reputation: 28703

I have added a few commands to the extension Select By v1.14.0 that allows to create and modify Multi Cursors with the keyboard.

  • selectby.addNewSelection : Add a new selection at an offset (default: 1)
  • selectby.moveLastSelectionActive : Modify (extend/reduce) the last selection by moving the Active position offset characters left/right (default: 1)
  • selectby.moveLastSelection : Move the last selection number of characters left/right (default: 1)
  • selectby.removeCursorBelow : remove the last selection

All 3 commands have 1 property, set in the args property of the key binding. If called from the Command Palette the value of offset is 1.

You can define a set of key bindings to use these commands.

By using a custom context variable (extension Extra Context v0.2.0) to set a mode, and use the when clause to determine if we use the default key binding for the arrow keys or our custom key bindings.

With the command extra-context.toggleVariable you can toggle the variable and thus the mode.

  {
    "key": "alt+F5", // or some other key combo
    "when": "editorTextFocus",
    "command": "extra-context.toggleVariable",
    "args": {"name": "multiCursorByKeyboard"}
  },
  {
    "key": "ctrl+alt+right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.addNewSelection",
    "args": {"offset": 1}
  },
  {
    "key": "shift+right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelectionActive",
    "args": {"offset": 1}
  },
  {
    "key": "shift+left",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelectionActive",
    "args": {"offset": -1}
  },
  {
    "key": "right",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelection",
    "args": {"offset": 1}
  },
  {
    "key": "left",
    "when": "editorTextFocus && extraContext:multiCursorByKeyboard",
    "command": "selectby.moveLastSelection",
    "args": {"offset": -1}
  }

Upvotes: 1

Related Questions