In VS Code, if I want to write a keyboard shortcut to send command
to the terminal, I can use the following in ~/.config/Code/User/keybindings.json
:
{
"key": "alt+p",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "commandu000D"
}
},
In this case, u000D
sends a carriage return symbol to the terminal, which effectively just runs the git push
command.
Now, I want to make a keyboard shortcut that presses the “up” key and then presses enter, to rerun the previous command. How do I do that?
For now, I have found a temporary workaround:
{
"key": "shift+alt+1",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "u0021u0021u000D"
}
},
Looking at Wikipedia’s List of Unicode characters, u0021
is unicode for !
, so this command sends !!
to the terminal, which in bash
expands to the most recently run command, and then sends carriage return to run the command. However, I’d like to find a cross platform solution, because to the best of my knowledge, !!
does not work the same way on Windows.
Another workaround is to copy the previous terminal command and paste it back into the terminal, but this has the disadvantage of wiping whatever was previously on the clipboard:
{
"key": "shift+alt+1",
"command": "runCommands",
"args": {
"commands": [
"workbench.action.terminal.copyLastCommand",
"workbench.action.terminal.paste",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "u000D"
}
}
]
}
},
I found a solution here, which is to use:
{
"key": "shift+alt+1",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "u001b[Au000d"
}
},
Here, u001b
is a unicode escape sequence, and [A
is an up arrow in xterm
. This works on Ubuntu, but I haven’t yet tested it in Windows.