I came up with this keybinding:
{
"key": "alt+x",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "u001b[Au000d" }
},
u001b
is an escape sequence to indicate the following characters have special meaning.
[A
is an up arrow. See, e.g., xterm function keys:
Cursor Up | CSI A
Cursor Down | CSI B
Cursor Right | CSI C
Cursor Left | CSI D
(the "CSI" refers to ESC
or u001b
or followed by a [
and stands for "Control Sequence Introducer" (CSI is 0x9b).)
So "CSI A" is u001b[A
which is equal to an up arrow which should cycle your terminal command list to the previous command.
u000d
is a return, so the command runs immediately.
Now Alt-x or whatever keybinding you choose will run the last shell command used, focus can be in the editor or the terminal.
For fun I put together this command:
"args": { "text": "u0012watchu001b[1;5C" }
That will send a Ctrl-R to the terminal which searches previous commands.
Then it will search for "watch
", and then Ctrl-rightArrow to go to the end of "watch
" where you could modify arguments if need be.
Or skip the Ctrl-rightArrow part (u001b[1;5C
) and do a return (u000d
) to run the command that was found anywhere in your history. Obviously, you will need a unique search term for that to work.
[Tested in powershell and git bash. Not tested elsewhere.]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…