0

One example of an operation consisting of multiple commands would be the following. I want to copy a line, making a duplicate of it, then, I need to return the the upper line and comment it out. The aim is to reach the following state.

previousStatement();
// statementToBeMultipliedAndCommentedOut();
statementToBeMultipliedAndCommentedOut();
nextStatement();

Today, I achieve that by a quick combination like this.

ctrl+c
ctrl+v
up
ctrl+k+c //commenting out
down

Is there a way to make a combo executing those keystrokes in a single key binding?

0

1 Answer 1

1

With vscode v1.77 Insiders now (Stable next week) you can run a sequence of commands WITHOUT an extension. This keybinding would do what you want:

{
  "key": "alt+c",             // choose your keybinding
  "command": "runCommands",
  "args": {
    "commands": [
      "editor.action.copyLinesDownAction",
      "cursorUp",
      "editor.action.addCommentLine",
      "cursorDown"
    ]
  }
}

For more, see How can I run multiple commands with a single VS Code keybinding without an extension?


[Before vscode v1.77]

You need a macro extension like multi-command so you can run a sequence of commands. There are other macro extensions out there. Using multi-command:

In settings.json:

"multiCommand.commands": [
  {
    "command": "multiCommand.commentDown",
    "sequence": [
      "editor.action.copyLinesDownAction",
      "cursorUp",
      "editor.action.addCommentLine",
      "cursorDown"
    ]
  }
]

The commands can be found in the Keyboard Shortcuts listing. Search on SO for "multi-command" to see some of things you can do with it. (I have no connection to it.)

Choose a keybinding in keybindings.json:

{
  "key": "ctrl+shift+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.commentDown" },
  "when": "editorTextFocus"
},
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.