I have a keybind that triggers a terminal command. The terminal command prompts for input, but in order to respond, I have to make an extra, unnecessary click to focus on the terminal. How can I avoid this so that when I trigger the keybind, the terminal command runs, and the VSCode focus automatically shifts to the terminal?
I found a solution, but I'd prefer not to press the keybind twice in a row; though it's still better than using the keybind and then clicking.
tasks.json
{
"tasks": [
{
"label": "Run Example Command",
"type": "shell",
"command": "powershell",
"args": [
"..."
],
"windows": {
"command": "powershell",
"args": [
"..."
]
}
}
]
}
keybindings.json
{
{
"key": "f4",
"command": "workbench.action.tasks.runTask",
"args": "Run Example Command"
},
{
"key": "f4",
"command": "workbench.action.terminal.focus",
"when": "taskRunning"
},
}
How can I make the terminal receive focus on the first F4 press?
Update Illustration
By default what my problem | With @Mark's second solution |
---|---|
I have a keybind that triggers a terminal command. The terminal command prompts for input, but in order to respond, I have to make an extra, unnecessary click to focus on the terminal. How can I avoid this so that when I trigger the keybind, the terminal command runs, and the VSCode focus automatically shifts to the terminal?
I found a solution, but I'd prefer not to press the keybind twice in a row; though it's still better than using the keybind and then clicking.
tasks.json
{
"tasks": [
{
"label": "Run Example Command",
"type": "shell",
"command": "powershell",
"args": [
"..."
],
"windows": {
"command": "powershell",
"args": [
"..."
]
}
}
]
}
keybindings.json
{
{
"key": "f4",
"command": "workbench.action.tasks.runTask",
"args": "Run Example Command"
},
{
"key": "f4",
"command": "workbench.action.terminal.focus",
"when": "taskRunning"
},
}
How can I make the terminal receive focus on the first F4 press?
Update Illustration
By default what my problem | With @Mark's second solution |
---|---|
1 Answer
Reset to default 1There may be two solutions - which I can't test without your actual powershell command and args.
Combine your commands into one keybinding using the runCommands
command:
{
"key": "alt+c",
"command": "runCommands",
"args": {
"commands": [
{
"command": "workbench.action.tasks.runTask",
"args": "echo"
},
"workbench.action.terminal.focus"
]
}
}
Or try adding presentation
options to your task:
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "powershell",
"args":
[...],
"presentation": {
"echo": true,
"reveal": "always", // note
"focus": true, // see if this works
"panel": "new",
"showReuseMessage": false,
"clear": true
}
}
],
F4
should be only one. – Sergey A Kryukov Commented Mar 12 at 12:12key
. What happens if you use a different key forworkbench.action.terminal.focus
? – Sergey A Kryukov Commented Mar 12 at 12:20workbench.action.tasks.runTask
with laterworkbench.action.terminal.focus
, when the task is already running, But it means that you have to perform focusing triggered by the running task or defer focusing. Note that you also make sure that the terminal is visible. In all cases, you still need one command and one key binding withF4
. Again, in my understanding, you can achieve something like that by writing a VSCode extension. – Sergey A Kryukov Commented Mar 12 at 12:24