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
!^ will get you the first param, !$ will get you the last param, !:n will get you the nth element.
!$
gets the last element of the previous command line argument.
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.
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
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
.
In Linux you can repeat commands to go back in history
Last command is:
mv foo bar
mv
foo
mv foo
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
5
4
3
2
1
echo
source: https://stackoverflow.com/a/34861762/3163120
bind -lp
bindkey -L
I'm keeping this up-to-date here: https://github.com/madacol/docs/blob/master/bash-zsh_TerminalShorcuts.md