Zsh和Bash中的Alt +数字+点和Alt +逗号 [英] Alt+number+dot and Alt+comma in Zsh and Bash

查看:186
本文介绍了Zsh和Bash中的Alt +数字+点和Alt +逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bash中,我们可以使用 Alt + number + .选择上一个的第n 个参数命令,以及 Alt + 选择前面的命令.他们循环浏览历史.

In Bash, we can use Alt+number+. to select the nth argument of previous commands, and Alt+, to select the previous commands. They cycles through the history.

例如:

$ ls 1 2
$ echo 10 20

现在按住Alt键,然后按0再按点,它将显示"echo".在不释放 Alt 的情况下,再次按.,它将显示"ls".在同一操作中使用1将显示10和1,以此类推.按Alt和逗号将显示历史记录中的整个命令行.另外, Alt .显示历史记录中命令的最后一个参数.

Now press and hold Alt, and press 0 then dot, it will show "echo". Without releasing Alt, press . again, it will show "ls". Use 1 in the same operation will show 10 and 1, etc. Pressing Alt and comma shows the whole command line in the history. Also Alt and . show the last argument of the commands in history.

请注意,所有这些操作只是将参数(或整个命令行)插入当前光标.他们不会更改当前命令行中已经存在的内容.

Note that all of these operations just insert the argument (or whole command line) to the current cursor. They don't change what's already there in the current command line.

我正在使用Zsh和最新的Oh-My-Zsh程序包,但似乎行为有所不同:

I am using Zsh and the latest Oh-My-Zsh package but it seems the behavior is different:

  • Zsh具有 Alt + ,以显示命令的最后一个参数.

  • Zsh has the Alt+, to show the last argument of commands.

Alt + 0 + .与Bash相同(显示逗号),但是 Alt + 数字 + .显示最后一个第n个 参数,即 Alt 上面的+ 1 + . 案例显示20和2.

The Alt+0+. is the same as Bash (shows the comman), but Alt+number+. shows the last nth argument, i.e., Alt+1+. in above case shows 20 and 2.

Alt + 不会显示历史记录中的全部命令.

The Alt+, doesn't display the whole commands in history.

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

How to do the same thing in Zsh? Thanks.

推荐答案

在寻找此功能时,我遇到了

Looking for this feature I came across this blogpost by Christian Neukirchen:

1.您可能知道M-.插入前一行的最后一个参数.有时,您想插入一个不同的参数.有几个选项:使用历史记录扩展,例如!:-2为前一行的第三个单词(如果不确定,请使用TAB展开),或使用M-.带有前缀参数:M-2 M-.

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

更好的是:

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

然后,M-m将复制当前行的最后一个单词,然后是第二个最后一个单词,依此类推.但是使用M-.您也可以返回行中!因此:

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.因此,从理论上讲,这应该是开箱即用的,就像克里斯蒂安所说.因此,我尝试了一下,是的,它 did 开箱即用.

In this text, M is referring to the Meta key, aka Alt. So in theory this should work out of the box, as Christian says. So I went to try this and yes, it did work out of the box.

负责此行为的zle小部件是insert-last-word -是ALT + . -和digit-argument -是ALT + Number.

The zle widgets in charge of this behavior are insert-last-word ─which is ALT+.─ and digit-argument ─which is 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命令的输出来更新原始问题,以帮助缩小问题的范围,或者直接在哦,我的zsh 及其详细信息.

so check that those appear and try again. You can update your original question with the output of the shown bindkey command to help narrow down the issue, or open one directly in oh-my-zsh with the details.

这篇关于Zsh和Bash中的Alt +数字+点和Alt +逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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