skip to Main Content

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.

2

Answers


  1. A hacky way that might work (not sure. put in keybindings.json):

    {
        "key": "", // TODO
        "command": "runCommands",
        "args": { "commands": [
            {
                "command": "type",
                "args": { "text": "nlorem ipsum" }, // TODO
            },
            "cursorHomeSelect",
            "editor.debug.action.selectionToRepl",
            "cursorUpSelect", // repeat as needed if text is multiline for some reason
            "cursorEndSelect",
            "deleteLeft",
        ]},
    },
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search