Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use arguments from previous command?

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 + .?

like image 323
Aman Jain Avatar asked Oct 24 '10 17:10

Aman Jain


People also ask

What command brings back the arguments and options from previous line?

In the command-line, you can press alt + . or esc - . It cycles through the last argument of your previous commands.

How do I run a previous command from history?

Another way to get to this search functionality is by typing Ctrl-R to invoke a recursive search of your command history. After typing this, the prompt changes to: (reverse-i-search)`': Now you can start typing a command, and matching commands will be displayed for you to execute by pressing Return or Enter.

Which bash shortcut or command copies the last argument of previous command?

Which Bash shortcut or command copies the last argument of previous commands? Pressing Esc+.


2 Answers

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

like image 200
user1953081 Avatar answered Oct 01 '22 10:10

user1953081


Just as M-. (meta-dot or esc-dot or alt-dot) is the readline function yank-last-arg, M-C-y (meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline function yank-nth-arg. Without specifying n, 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

$ echo a b c d e f g a b c d e f g 

Now at the next prompt, type echo (with a following space), then

Press Alt-Ctrl-y and you'll now see:

$ echo a 

without pressing Enter yet, do the following

Press Alt-3 Alt-Ctrl-y

Press Alt-- 2 Alt-Ctrl-y

Now you will see:

$ echo ace 

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:

bind '"\eY": "\e2\e."' 

To make it persistent, add this line to your ~/.inputrc file:

"\eY": "\e2\e." 

Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.

like image 22
Dennis Williamson Avatar answered Oct 01 '22 10:10

Dennis Williamson