gpt4 book ai didi

shell - 进行键绑定(bind)以运行上一个或最后一个 shell 命令

转载 作者:行者123 更新时间:2023-12-03 01:01:34 28 4
gpt4 key购买 nike

我经常想快速重新运行我使用的最后一个 shell 命令。

我知道您可以将焦点转移到终端,向上箭头并输入,但我认为必须有比这三个步骤更好的方法。
sendSequence vscode 中的命令变得越来越强大,因此我寻找一种方法来创建可以快速运行最后一个 shell 命令的键绑定(bind)。

来自 sendSequence documentation :

Send text from a keybinding

The workbench.action.terminal.sendSequence command can be used to send a specific sequence of text to the terminal, including escape sequences. This enables things like sending arrow keys, enter, cursor moves, etc. The example below shows the sorts of things you can achieve with this feature, it jumps over the word to the left of the cursor (Ctrl+Left arrow) and presses backspace:


{
"key": "ctrl+u",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001b[1;5D\u007f" }
}

This feature supports variable substitution.

Note that the command only works with the \u0000 format for using characters via their character code (not \x00).



terminal supports variables substitution :
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": ". ${file}" }
}

例如,见 run files in terminal :
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "node '${file}'\u000D" }
}

最佳答案

另见 https://stackoverflow.com/a/70900927/836330有关如何查看某些受支持的 shell 和操作系统的最近终端命令的快速选择(从 v1.64 开始预览)。

我想出了这个键绑定(bind):

{
"key": "alt+x",

"command": "workbench.action.terminal.sendSequence",

"args": { "text": "\u001b[A\u000d" }
},
  • \u001b是一个转义序列,表示以下字符具有特殊含义。
  • [A是向上箭头。参见,例如,xterm function keys :
    Cursor Up    | CSI A
    Cursor Down | CSI B
    Cursor Right | CSI C
    Cursor Left | CSI D

  • (“CSI”指的是 ESC\u001b 或后跟 [,代表“控制序列引入器”(CSI 为 0x9b)。)
    所以“CSI A”是 \u001b[A这等于一个向上箭头,它应该将您的终端命令列表循环到上一个命令。
  • \u000d是返回,所以命令立即运行。

  • 现在 Alt-x 或您选择的任何键绑定(bind)将运行最后使用的 shell 命令,焦点可以在编辑器或终端中。
    为了好玩,我把这个命令放在一起:
    "args": { "text": "\u0012watch\u001b[1;5C" }    
    这会将 Ctrl-R 发送到搜索先前命令的终端。
    然后它将搜索“ watch”,然后按 Ctrl-rightArrow 转到“ watch”的末尾,如果需要,您可以在其中修改参数。
    或者跳过 Ctrl-rightArrow 部分 ( \u001b[1;5C ) 并执行返回 ( \u000d ) 以运行在历史记录中的任何位置找到的命令。显然,您需要一个唯一的搜索词才能使其正常工作。
    [在 powershell 和 git bash 中测试。未在其他地方测试。]

    关于shell - 进行键绑定(bind)以运行上一个或最后一个 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55336497/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com