How to use arguments from previous command?

前端 未结 11 1760
一个人的身影
一个人的身影 2020-12-04 04:35

I know that Esc + . gives you the last argument of the last command.

But I\'m interested in first argument of the last command. Is there a key

相关标签:
11条回答
  • 2020-12-04 04:45

    !^ will get you the first param, !$ will get you the last param, !:n will get you the nth element.

    0 讨论(0)
  • 2020-12-04 04:46

    !$ gets the last element of the previous command line argument.

    0 讨论(0)
  • 2020-12-04 04:47

    If you are on a mac you will tend to get extended characters with ctrl+letter. I have my right-of-space-bar-option key defined as meta in my terminal (iTerm2) set up. This means I use the key to navigate by word and pull parameters from previous commands.

    0 讨论(0)
  • 2020-12-04 04:48

    You can also get arguments from any command in your history!

    
    $ echo a b c d e f g
    a b c d e f g
    $ echo build/libs/jenkins-utils-all-0.1.jar
    build/libs/jenkins-utils-all-0.1.jar
    $ history | tail -5
      601  echo build/libs/jenkins-utils-all-0.1.jar
      602  history | tail -10
      603  echo a b c d e f g
      604  echo build/libs/jenkins-utils-all-0.1.jar
      605  history | tail -5
    $ echo !-3:4
    echo d
    d
    $ echo !604:1
    echo build/libs/jenkins-utils-all-0.1.jar
    build/libs/jenkins-utils-all-0.1.jar
    

    0 讨论(0)
  • 2020-12-04 04:53

    The method described at the end of the accepted answer also works with the zeroth argument for me. I have these lines in my ~/.inputrc:

    "\en": "\e0\e."
    "\em": "\e1\e."
    "\e,": "\e2\e."
    

    \e2\e. has the advantage over \e2\e\C-y that it cycles through previous commands if it is pressed repeatedly instead of inserting the second argument of the previous command multiple times.

    To insert the whole previous command, you can type !!\e^. \e^ is history-expand-line.

    0 讨论(0)
  • 2020-12-04 04:54

    Tested on Ubuntu 18.04


    To insert previous arguments:

    • Alt+.: insert last argument from last command.
    • Alt+#+.: insert #nth last argument from last command.
    • Alt+- , # , Alt+., zsh: Alt+-+#+.: insert #nth first argument from last command.

    In Linux you can repeat commands to go back in history

    Example:

    Last command is:

    mv foo bar
    
    • Alt+0+.: insert first argument of last command = mv
    • Alt+2+.: insert last 2nd argument of last command = foo
    • up , Ctrl+w: last command without the last word = mv foo

    General shortcuts

    • Ctrl+w: removes last word from cursor
    • Alt+d: removes next word from cursor
    • Ctrl+k: cuts everything after cursor
    • Ctrl+u, zsh: Alt+w: cuts everything before cursor
    • zsh: Ctrl+u: cuts the entire command (In bash you can combine Ctrl+u , Ctrl+k)
    • Ctrl+y: paste characters previously cut with Ctrl+u and Ctrl+k
    • Ctrl+_: undo last edit (very useful when exceeding Ctrl+w)
    • Ctrl+left: move to last word
    • Ctrl+right: move to next word
    • home or Ctrl+a: move to start of line
    • end or Ctrl+e: move to end of line

    To iterate through the arguments in a previous command

    only works in zsh

    run or add this to your ~/.zshrc

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

    Now use Alt+. to go as back as you want, then use Alt+: to iterate through the arguments

    Assuming last command is

    echo 1 2 3 4 5
    
    • Alt+.: 5
    • Alt+.+:: 4
    • Alt+.+:+:: 3
    • Alt+.+:+:+:: 2
    • Alt+.+:+:+:+:: 1
    • Alt+.+:+:+:+:+:: echo

    source: https://stackoverflow.com/a/34861762/3163120

    To see all shortcuts available

    • bash: bind -lp
    • zsh: bindkey -L

    I'm keeping this up-to-date here: https://github.com/madacol/docs/blob/master/bash-zsh_TerminalShorcuts.md

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