在Zsh和Bash中,Alt+数字+句点以及Alt+逗号的作用是什么?

10
在Bash中,我们可以使用Alt+数字+.来选择先前命令的第n个参数,而Alt+,则可以选择先前的命令。它们会循环浏览历史记录。
例如:
$ ls 1 2
$ echo 10 20

现在按住Alt键,然后按0再按点号,它将显示“echo”。不释放Alt,再次按下.,它将显示“ls”。在同一操作中使用1将显示10和1等。按下Alt和逗号会显示历史命令行中的整个命令行。同时,Alt.会显示历史命令中的最后一个参数。
请注意,所有这些操作只是将参数(或整个命令行)插入到当前光标处。它们不会改变当前命令行中已有的内容。
我正在使用Zsh和最新的Oh-My-Zsh软件包,但似乎行为不同:
  • Zsh具有Alt+,以显示命令的最后一个参数。

  • Alt+0+.与Bash相同(显示命令),但Alt+number+.显示历史记录中的第n个参数,即上面的例子中Alt+1+.显示20和2。

  • Alt+,不会显示历史命令中的整个命令。

如何在Zsh中执行相同的操作?谢谢。

默认情况下,我不知道Alt-comma绑定到任何东西。 “历史记录中的上一个命令”的默认绑定是ctrl-p(如果您回退太远,则ctrl-n是历史记录中的下一个命令等)。 (例如,请参见bash手册中的[历史记录命令](http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-History)。) bind -q previous-history对您输出什么? (我得到previous-history can be invoked via "\C-p", "\eOA", "\e[A". - Etan Reisner
学习zlebindkey,它们是与http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html相关的编程内容。ZLE比bash/readline更强大和可定制,这是我唯一想说的事情。 - 4ae1e1
Ctrl-p和Ctrl-n的工作方式与Bash相同。它们只是用历史记录中的上一个/下一个命令替换当前的命令行。我想我必须学习zle和bindkey了。只是有点惊讶这些有用的快捷键不可用。 - user2847598
1个回答

11

我在寻找这个功能时,发现了Christian Neukirchen的这篇博客文章

1. You probably know M-. to insert the last argument of the previous line. Sometimes, you want to insert a different argument. There are a few options: Use history expansion, e.g. !:-2 for the third word on the line before (use TAB to expand it if you are not sure), or use M-. with a prefix argument: M-2 M-.

Much nicer however is:

autoload -Uz copy-earlier-word
zle -N copy-earlier-word
bindkey "^[m" copy-earlier-word

Then, M-m will copy the last word of the current line, then the second last word, etc. But with M-. you can go back in lines too! Thus:

% echo a b c
% echo 1 2 3
% echo <M-.><M-.><M-m>
% echo b

Man, I wish I knew that earlier!

在这段文字中,M 指的是 Meta 键,也就是 Alt。理论上来说,这应该可以直接使用,正如 Christian 所说。所以我去尝试了一下,没错,它确实可以直接使用。
负责这种行为的 zle 小部件是 insert-last-word ─ 也就是 ALT+. ─ 和 digit-argument ─ 也就是 ALT+数字
以下是相关的 bindkey 输出:
$ bindkey -L | grep '\^\[[.0-9]'
bindkey "^[." insert-last-word
bindkey "^[0" digit-argument
bindkey "^[1" digit-argument
bindkey "^[2" digit-argument
bindkey "^[3" digit-argument
bindkey "^[4" digit-argument
bindkey "^[5" digit-argument
bindkey "^[6" digit-argument
bindkey "^[7" digit-argument
bindkey "^[8" digit-argument
bindkey "^[9" digit-argument

请检查这些是否出现,然后再试一次。您可以更新原始问题,并使用显示的 bindkey 命令输出来帮助缩小问题范围,或者直接在 oh-my-zsh 中打开一个带有详细信息的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接