Alt+number+dot and Alt+comma in Zsh and Bash

后端 未结 1 754
野的像风
野的像风 2021-01-12 16:22

In Bash, we can use Alt+number+. to select the nth argument of previous commands, and Alt+, to select the pre

相关标签:
1条回答
  • 2021-01-12 16:42

    Looking for this feature I came across this blogpost by 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!

    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.

    The zle widgets in charge of this behavior are insert-last-word ─which is ALT+.─ and digit-argument ─which is ALT+Number.

    Here's the relevant bindkey output:

    $ 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
    

    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.

    0 讨论(0)
提交回复
热议问题