How can you reverse the direction of alt-period in Bash?

10

8

In Bash, you can press Alt-. to insert the last argument of the previous command. If you press it multiple times, it cycles through the last arguments of all of the commands in your history. However, sometimes I press it too many times, and I want to reverse the direction of the cycling, hence go forwards. Is this possible?

(i.e. if you reverse-i-search with Ctrl-r, you can change directions with Ctrl-s. What is the analogue for Alt-.?)

Sparhawk

Posted 2013-05-10T11:03:27.207

Reputation: 1 201

Answers

11

If you supply a negative argument to Alt-., it reverses direction. The easiest way to do that (with standard keybindings) is Alt-- (equivalent to an argument of -1).

So, after one or more Alt-. keypresses, pressing Alt-- will cause the next Alt-. to go in the reverse direction. (Just ignore the argument dialog which appears when you press Alt--.)

rici

Posted 2013-05-10T11:03:27.207

Reputation: 3 493

Thank you! I have been wondering how to do this for ages. – dotancohen – 2014-12-02T10:38:58.340

1I'm not sure what you mean exactly. Pressing Alt+- removes the command-line prompt and inserts (arg: -1) instead. The command itself is unchanged. – Sparhawk – 2013-05-12T06:23:55.400

1Thenext alt-. (with the -1 arg) will go forwards. – rici – 2013-05-12T06:46:21.770

That's great! I've edited the answer to make it a bit clearer. What does alt+- do exactly? – Sparhawk – 2013-05-12T12:35:42.903

1@Sparhawk: as I said, alt - produces an argument of -1 for the next readline command. Or, more precisely, it starts a negative argument, so alt -, alt2would produce an argument of-2. In the case ofalt., only the sign of the argument matters, and only if it is negative, soalt-is sufficient. (The firstalt.` handles its argument differently. See the bash manual.) – rici – 2013-05-12T16:12:46.417

Thank you. I often find man bash slightly opaque for such situations, but I think it's clarified it a little here. – Sparhawk – 2013-05-14T02:22:05.167