Assign terminal commands to a keybinding.
Open your keybindings.json (Command Palette => 'Preferences: Open Keyboard Shortcut File') and add an entries like these:
{
"key": "cmd+ctrl+r",
"command": "terminalCommandKeys.run",
"args": {
"cmd": "echo 'hello world'"
}
},
{
"key": "cmd+shift+r",
"command": "terminalCommandKeys.run",
"args": {
"cmd": "echo 'hello from file: ${file}'",
"newTerminal": false,
"saveAllFiles": true,
"showTerminal": true,
"focus": true,
"terminalName": "some terminal name"
}
}
The args
entry has these entries:
This is the command that will be passed to the terminal when it is opened. The following substitutions are available to be used:
- ${line}
- ${relativeFile}
- ${file}
- ${workspaceRoot}
Here are some examples:
- "cmd": "rspec ${file}"
- "cmd": "rspec ${file}:${line}"
If you need to run different commands for different file types, but wish to use the same keycombo, check out vscode's "when" clauses.
If true, the terminal is destroyed and a fresh terminal is created for each run. You cannot scroll up to review previous commands/results.
If false, re-uses the same terminal for each command run. You can scroll up to review previous commands/results.
If true, saves all files before running the command.
If false, does not save all files before running the command.
If true, ensures that the terminal is showing when running the command.
If false, does not change the visibility of the terminal when running the command.
If true, ensures that the terminal is focused when running the command.
If false, focus is not applied to the terminal when running the command.
Allows you to set a custom name for the terminal associated with this command, allowing you to associate different commands with different terminals. Thanks to @qzsiniong.
This extension inspired by run-in-terminal and send-to-terminal.
The differences (at this time):
- All config lives in keybindings file (no entries in user settings)
- Configuration per command
- Option to save all files before execution
- Option to reveal the terminal before execution
- Option to use a new terminal for each execution
- Option to custom terminal name
- Scrolls to bottom of terminal when running a command