1

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 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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",
    ]},
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.