visual-studio-codeshortcutvscode-keybinding

How can I execute a fixed command in VS Code's debug console with a keyboard shortcut?


I am using Native Debug with GDB under VS Code. I can type any GDB command in the debug console. How can I run a fixed command in the debug console (not the terminal) using a shortcut?

I was only able to find editor.debug.action.selectionToRepl (also known as "Evaluate in Debug Console") but this executes the currently selected text in the debug console. I want to execute a hardcoded command.


Solution

  • Because of this issue: Send Text to replc command

    not being worked on there is no good way to accomplish what you want. The only thing to do is get the code you want to run into the clipboard and then paste it.

    But here is another way to do that, using an extension I wrote that can run the vscode api and javascript. Using Find and Transform make this keybinding:

    {
      "key": "alt+r",                 // whatever you want
      "command": "findInCurrentFile",
      "args": {
        "runWhen": "onceOnNoMatches",   // cursor should not be on a word
        "run": [
          "$${",
            "await vscode.env.clipboard.writeText('2*3');",  // your code
            "await vscode.commands.executeCommand('workbench.panel.repl.view.focus');",
            "await vscode.commands.executeCommand('debug.replPaste');",
            "await vscode.commands.executeCommand('repl.action.acceptInput');",
          "}$$",
        ]
      }
    }
    

    Your code to run goes into the await vscode.env.clipboard.writeText('2*3') line. \\u000D would add a newline to the repl.

    The only restriction is that the cursor should not be on any word (I will look into relaxing that requirement) because that will cause a match and the run argument will not run.

    Later I will show how that keybinding can also be a setting, rather than a keybinding alone (and thus the resulting command will appear in the Command Palette).

    The last command repl.action.acceptInput makes the repl input run immediately which you may or may not want.

    run code from a keybinding in the debug repl

    As written the keybinding can be run from anywhere, but you can add a when clause to restrict that if you wish.