How to use arguments from previous command?

前端 未结 11 1774
一个人的身影
一个人的身影 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:57

    Basically it has a use in yanking previous (command's) arguments.

    For instance, if the following command is issued:

    echo Hello, world how are you today?
    

    then, Hello, will be the first argument, and today? the sixth, that is the last one; meaning it can be referenced by typing:

    Alt+6 followed by Ctrl-Alt-6


    Ctrl is traditionally denoted as a hat character ^ prepended to keys names, and Alt as M- that is Meta prefix.

    So the above shortcut can be redefined as ^My to yank.


    Also, there is hats substitution shortcut in the command line:

    echo Hello, world!
    
    ^Hello^Bye
    
    Bye, world!
    

    to substitute the previous command's first matched string, meaning:

    Hello, world! Hello, people!
    
    ^Hello^Bye
    

    would result in:

    Bye, world! Hello, people!
    

    leaving the second match (hello) unchanged.

    Note: Do not leave space between hats, or the operation won't work.


    The above is just a shortcut for:

    !:s/Hello/Bye
    

    event-level(*) substitution for the first found (matched) string in the previous command, while prefixing the first part with the g switch will apply to the whole line globally:

    echo Hello, world! Hello, people!
    
    !:gs/Hello/Bye
    
    Bye, world! Bye, people!
    

    as usually being done in other related commands such as sed, vi, and in regex (regular expression) - a standart way to search (match string).

    No, you can't do !:sg/Hello/Bye or !:s/Hello/Bye/g here, that's the syntax!


    • ! is for events; event might be understood as command output or operation done in the commands history.

    That's what I understood by using it myself and trying things on my own from what I read from various sources including manual pages, blogs, and forums.

    Hope it will shed some light into mysterious ways of bash, the Bourne-Again shell (a play on sh shell, which itself is called Bourne shell after its inventor's last name), what is default shell in many distributions including servers (server OS's).

提交回复
热议问题