Cycle through the last argument of previous commands in Bash

In Bash, when I want to repeat the last argument of the previous command, I usually type !$. I just discovered that you can also use ALT+. to cycle through the last argument of previous commands.

Advertisements

5 Responses to Cycle through the last argument of previous commands in Bash

  1. matt says:

    Along those same lines, if you know you typed a command in the past but don’t want to up-arrow or Alt_. you can do Ctrl+r (then type what you’re looking for) to reverse search through the command history. Press Ctrl+r again (while still within the first search) to search back further.

  2. George says:

    What operating system were you on at the time? Is is suppose to work in all implementations of bash(1).

    I am using iTerm (or Terminal.app) on a Macintosh 10.6 (Snow Leopard) and neither of the Alt commands mentioned (ALT+ and ALT_) work.

  3. davitenio says:

    It works for me on Ubuntu with GNU bash, version 3.2.39(1)-release. The command is described as ‘yank-last-arg’ in the bash man page. From the man page:

    yank-last-arg (M-., M-_)
    Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the “!$” history expansion had been specified.

    The meta key is probably not ALT in iTerm; try ctrl+. or esc+. or cmd+. or some other key that could possibly be the meta key (note: do not type ‘+’, alt+. means holding the alt key while pressing the ‘.’ key).

  4. marc says:

    Alt. doesn’t work for me, either, using Terminal in Mac (although I’ve played with settings extensively). !$ does work, however.

  5. Ian says:

    >> Alt. doesn’t work for me, either, using Terminal in Mac.

    By default it doesn’t but you can fix this:

    Run the Terminal app and choose “Terminal>preferences” from the menu.

    In the new Preferences window go to “Settings>Keyboard”

    Tick the “Use option as meta key” box.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: