I am starting to learn some Regex, therefore I use this command repeatedly:

grep pattern /usr/share/dict/american-english 

Only the part with pattern changes, so I have to write the long expression "/usr/share/dict/american-english" again and again.

Someone made the remark that it is possible to expand an argument of a command from the command history by typing cryptic character combinations instead of the full expression. Could you tell me those cryptic character combinations ?

  • Is this about the second argument or the last argument?
    – mikeserv
    Apr 5, 2015 at 4:39
  • in this very case the last argument happens to be the second, I did not make this distinction clear. Apr 5, 2015 at 12:00
  • In that case, the bash, dash, zsh, mksh, and ksh shells (at least, that I know about) will all recognize the $_ last arg special parameter (some do so only in interactive mode, though). It just expands like a variable: grep somepattern filename; grep otherpattern "$_". You can usually see its contents in the output of set.
    – mikeserv
    Apr 6, 2015 at 6:03

2 Answers 2


You can use <M-.> (or <Esc>. if your Meta key is being used for something else), that is, Meta-dot (or <esc> dot), where Meta is usually the Alt key, to recall the last argument of the previous command. So, first you would type

$ grep foo /usr/share/dict/american-english

And then if you wanted to grep for something else, you would type

$ grep bar

After typing a space and then Esc. (that is, first pressing the escape key, and then the period key):

$ grep bar /usr/share/dict/american-english

You can also use either of the following:

$ grep bar !:2
$ grep bar !$

Where !:2 and !$ mean "second argument" and "last argument" respectively.

  • This is for bash and shells that use readline using emacs (default) bindings: with editing-mode vi set you need to add a binding to ~/.inputrc for yank-last-arg and for Zsh you need to bind insert-last-word...
    – jasonwryan
    Apr 4, 2015 at 20:40
  • @jasonwryan, if it's the last arg you want, then in any of (interactive) zsh, bash, ksh, mksh, or dash shells you can just use $_. POSIXLY that binding is already set, though, to ESC _ when set -o vi is in effect.
    – mikeserv
    Apr 5, 2015 at 4:44
  • @mikeserv Thanks Mike, I must have rebound it for compatability...
    – jasonwryan
    Apr 5, 2015 at 5:00
  • @jasonwryan - no problem - this just happens to be the only shell keyboard trick I know - I suck at remembering/configuring all of those quick-type shortcuts. If you don't mind, compatibility w/ what? Is there some program you use that does it differently? Oh, I can also remember ESC @letter for the _letter alias binding - but only because I still can't figure out how to make it work w/ bsd editline.
    – mikeserv
    Apr 5, 2015 at 5:44
  • @mikeserv I bind it that way so that it is the same no matter whether I am using Bash or Zsh (both in vi mode) or on a machine with Bash in Emacs mode (it is one of the bindings I really hammer).
    – jasonwryan
    Apr 5, 2015 at 6:26

In general (with Bash) <Esc> N <Esc> . will recall the N'th argument from the previous command into the current command line (where N is 0-based).

For the second argument from the last command, that would be:

<Esc> 1 <Esc> .

If you wanted the third argument, then

<Esc> 2 <Esc> .

... and so on.

As others have mentioned, if you want the last argument from the previous command, there's a specific shortcut for that in just:

<Esc> .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .