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 binding to do so?
On the same lines, is there a generic way of getting the nth argument from the last command?
I know that in a bash script, you can use $0
, $1
etc., but these don't work on the commandline.
Also, what about iterating through the 0th argument of previous commands, like we can do with the last argument by continuously pressing Esc + .?
Basically it has a use in yanking previous (command's) arguments.
For instance, if the following command is issued:
then,
Hello,
will be the first argument, andtoday?
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 asM-
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:
to substitute the previous command's first matched string, meaning:
would result in:
leaving the second match (
hello
) unchanged.The above is just a shortcut for:
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:as usually being done in other related commands such as
sed
,vi
, and inregex
(regular expression) - a standart way to search (match string).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 onsh
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).Just as
M-.
(meta-dot or esc-dot or alt-dot) is the readline functionyank-last-arg
,M-C-y
(meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline functionyank-nth-arg
. Without specifyingn
, it yanks the first argument of the previous command.To specify an argument, press Escape and a number or hold Alt and press a number. You can do Alt--to begin specifying a negative number then release Alt and press the digit (this will count from the end of the list of arguments.
Example:
Enter the following command
Now at the next prompt, type
echo
(with a following space), thenPress Alt-Ctrl-y and you'll now see:
without pressing Enter yet, do the following
Press Alt-3 Alt-Ctrl-y
Press Alt-- 2 Alt-Ctrl-y
Now you will see:
By the way, you could have put the
echo
on the line by selecting argument 0:Press Alt-0 Alt-Ctrl-y
Edit:
To answer the question you added to your original:
You can press Alt-0 then repeatedly press Alt-. to step through the previous commands (arg 0). Similarly Alt-- then repeating Alt-. would allow you to step through the previous next-to-last arguments.
If there is no appropriate argument on a particular line in history, the bell will be rung.
If there is a particular combination you use frequently, you can define a macro so one keystroke will perform it. This example will recall the second argument from previous commands by pressing Alt-Shift-Y. You could choose any available keystroke you prefer instead of this one. You can press it repeatedly to step through previous ones.
To try it out, enter the macro at a Bash prompt:
To make it persistent, add this line to your
~/.inputrc
file:Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.
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
:\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^
ishistory-expand-line
.!$
gets the last element of the previous command line argument.!^ will get you the first param, !$ will get you the last param, !:n will get you the nth element.
You can also get arguments from any command in your history!