Bash – Go forward in bash argument history

bashkeyboard shortcutsreadline

I know about Alt + . to show the previous argument in bash.

But what if I went past the desired argument (pressing Alt + . one too many times) and I want to go forward in history?

Is there a key combination for that?

Best Answer

The Alt-. runs readline function yank-last-arg:

yank-last-arg (M-., M-_)

Insert the last argument to the previous command (the last word of the previous history entry). With a numeric argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last word (or the word specified by the argument to the first call) of each line in turn. Any numeric argument supplied to these successive calls determines the direction to move through the history. A negative argument switches the direction through the history (back or forward). The history expansion facilities are used to extract the last word, as if the "!$" history expansion had been specified.

So to reverse its meaning one has to supply a negative argument to it. This can be done via other functions: digit-argument or universal-argument. The first is easier to use because it is already bound to Alt-[[:digit:]] and Alt-- for negative arguments.

So, to go backward in history run Alt-.. To go forward run Alt-- Alt-. and repeat with just Alt-.. To go backward again, switch directions again with Alt-- Alt-..

Related Question