DEV Community

sato_u
sato_u

Posted on • Originally published at satoslabo.com on

Quick Terminal Command Retry with VS Code Keybinding - Execute Commands Without Terminal Focus

Rerunning Previous Terminal Commands with a Keyboard Shortcut

You can set up a custom keybinding in VS Code to quickly re-execute your previous terminal command.
Add the following to your VS Code keybindings.json file:

{
  "key": "ctrl+p",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "\u001b[A\u000D" },
  "when": "editorTextFocus"
}
Enter fullscreen mode Exit fullscreen mode

Key Benefits

One of the best features of this keybinding is that you can execute terminal commands without switching focus to the terminal. This means you can stay in the editor while editing your code and seamlessly re-run commands without interrupting your workflow.

How It Works

This keybinding uses the workbench.action.terminal.sendSequence command to:

  1. Send the escape sequence for the up arrow key (\u001b[A) to recall the previous command
  2. Send a carriage return (\u000D) to execute it

Top comments (0)