进行键绑定以运行上一个或上一个 shell 命令 [英] Make a keybinding to run previous or last shell commands

查看:62
本文介绍了进行键绑定以运行上一个或上一个 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道您可以将焦点转移到终端,向上箭头并输入,但我认为必须有比这三个步骤更好的方法.

vscode 中的 sendSequence 命令变得越来越强大,所以我寻找一种方法来创建一个可以快速运行最后一个 shell 命令的键绑定.

来自 sendSequence 文档:

<块引用>

通过键绑定发送文本

workbench.action.terminal.sendSequence 命令可用于发送到终端的特定文本序列,包括转义序列.这使诸如发送箭头键、输入、光标之类的事情成为可能移动等. 下面的例子展示了你可以做的事情使用此功能实现,它会跳过左侧的单词光标(Ctrl+向左箭头)并按下退格键:

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

<块引用>

此功能支持变量替换.

请注意,该命令仅适用于 \u0000 格式,以便使用字符通过它们的字符代码(不是 \x00).

参见终端支持变量替换:

<代码>{"key": "ctrl+shift+t","command": "workbench.action.terminal.sendSequence","args": { "text": ". ${file}" }}

例如,请参阅在终端中运行文件:

<代码>{"key": "ctrl+shift+t","command": "workbench.action.terminal.sendSequence","args": { "text": "node '${file}'\u000D" }}

解决方案

我想出了这个键绑定:

<代码>{"key": "alt+x","command": "workbench.action.terminal.sendSequence","args": { "text": "\u001b[A\u000d" }},

  1. \u001b 是一个转义序列,表示以下字符具有特殊含义.

  2. [A 是一个向上的箭头.参见,例如,xterm 功能键:

    光标向上 |CSIA光标向下 |CSI B光标向右 |CSI C光标向左 |CSI D

(CSI"指的是 ESC\u001b 或后跟 [ 并代表控制序列引入器"(CSI是 0x9b))

所以CSI A"是\u001b[A,它等于一个向上的箭头,它应该将你的终端命令列表循环到上一个命令.

  1. \u000d 是一个返回,所以命令会立即运行.

现在 Alt-x 或您选择的任何键绑定将运行最后使用的 shell 命令,焦点可以在编辑器或终端中.

为了好玩,我把这个命令放在一起:

"args": { "text": "\u0012watch\u001b[1;5C" }

这会将 Ctrl-R 发送到搜索先前命令的终端.

然后它会搜索watch",然后Ctrl-rightArrowwatch" 如果需要,您可以在其中修改参数.

或者跳过 Ctrl-rightArrow 部分 (\u001b[1;5C) 并返回 (\u000d) 运行在历史记录中任何位置找到的命令.显然,您需要一个独特的搜索词才能使其发挥作用.

[在 powershell 和 git bash 中测试.未在其他地方测试.]

I frequently want to quickly re-run the last shell command that I used.

I know you can shift focus to the terminal, up arrow and enter but I thought there must be a better way than these three steps.

The sendSequence command in vscode is getting more powerful and so I looked for a way to create a keybinding that will run the last shell command quickly.

From 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).

See terminal supports variables substitution:

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

For example, see run files in terminal:

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

解决方案

I came up with this keybinding:

{
  "key": "alt+x",

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

  "args": { "text": "\u001b[A\u000d" }
},

  1. \u001b is an escape sequence to indicate the following characters have special meaning.

  2. [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.

  1. \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": "\u0012watch\u001b[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.]

这篇关于进行键绑定以运行上一个或上一个 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆