0

How can I create the following hotkey in VScode:

  1. while editing a file (curser/ focus within text file), execute the last terminal command
  2. bring curser/ focus back to where it was in the text file, so i can seamlessly continue coding

This is intended for debugging code and running the code via terminal efficiently

Mark
  • 143,421
  • 24
  • 428
  • 436
Dr-Nuke
  • 368
  • 3
  • 11

2 Answers2

0

I simplified my previous answer. Nothing needed in the settings.

You will need a macro extension like multi-command. Using multi-command, make this keybinding:

{
  "key": "alt+x",     // whatever keybinding you want
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [

       // you may be able to use this command instead of the sendSquence command, see below
       // "workbench.action.terminal.runRecentCommand",
      {
        "command": "workbench.action.terminal.sendSequence",
           // send an uparrow to the terminal shell
        "args": {"text": "\u001b[A\u000D"}, 
      },
      "workbench.action.focusActiveEditorGroup",  // return focus to last active editor
    ]
  },
     // if you want this to work only when focus is in an editor
    "when": "editorFocus" 
}

You can use the following command instead of the sendSquence command object above

"workbench.action.terminal.runRecentCommand",

if you are using one of the currently supported shells - and you have enabled shell integration - see https://stackoverflow.com/a/55336498/836330 for more on this.

For explanation of the sendSequence command part above, see https://stackoverflow.com/a/55336498/836330 (Make a keybinding to run previous or last shell commands).

Mark
  • 143,421
  • 24
  • 428
  • 436
0

You can also add some other helpful commands to the beginning of Mark's command sequence!

    {
        "key": "alt+x",
        "command": "extension.multiCommand.execute",
        "args": {
            "sequence": [
                "workbench.action.files.saveAll",
                "workbench.action.terminal.scrollToBottom",
                {
                    "command": "workbench.action.terminal.sendSequence",
                    "args": {"text": "\u001b[A\u000D"},
                },
                "workbench.action.focusActiveEditorGroup",
            ]
        },
        "when": "editorFocus"
    }
  • I suppose you couldn't just comment Mark's answer due to the lack of reputation... Anyway, add some description explaining how and why you improved the original answer, that will make yours even more valuable – Ilario Aug 04 '21 at 10:59