在 Bash 中,我们可以使用 Alt+number+. 来选择上一个参数的第 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 相同(显示comman),但是 Alt+number+. 显示最后 nth 个参数,即 Alt +1+. 在上面 案例显示 20 和 2。
Alt+, 不显示历史中的全部命令。
如何在 Zsh 中做同样的事情?谢谢。
最佳答案
我在寻找这个功能时遇到了 this blogpost作者: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
。所以理论上这应该开箱即用,正如克里斯蒂安所说。所以我去尝试了一下,是的,它确实开箱即用。
负责此行为的 zle 小部件是 insert-last-word
─ 即 ALT
+.
─ 和 digit -argument
─即 ALT
+Number
。
这是相关的 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 中打开一个问题。包含详细信息。
关于bash - Zsh 和 Bash 中的 Alt+数字+点和 Alt+逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571398/