Sometimes I need to reuse certain long arguments like paths.

If I use history search (CTRL+R), I may find some command that contains the path, but I still need to delete anything else in the old command, and then move cursor around to write the new command. Worse is that I cannot do this for more than once in a line.

I wonder if any shell provides an argument-level history search?

Tab-completion that looks into history commands (and arguments) somehow helps. But completion has to match the prefix you type, instead of any substrings.

  • I was thinking of tab-completion, too. What is the difference between "the prefix" and "any substring"?
    – michas
    Jan 20, 2013 at 3:25
  • Say I have a long path /usr/local/foo/bar/baz/my-server. What is interesting is "my-server". If I am using tab-completion, I will have to remember what is the front part of the path, which is what I am trying to avoid..
    – user716468
    Jan 20, 2013 at 23:54

5 Answers 5


If I understand correctly, you are after dynamic-complete-history (see e.g. man bash or http://www.cl.cam.ac.uk/local/sys/unix/applications/bash/), by default bound to M-Tab.

  • Their tokenizehist example there looks close to what the OP was looking for. Jul 24, 2015 at 15:37

In zsh, with new-style completion active (i.e. with compinit in your .zshrc), the _history_complete_word function provides this feature. It is accessible through two widgets: _history-complete-older and _history-complete-newer, bound to ESC , and ESC / by default. These two commands cycle through all the words in the shell history that have the current word as a prefix.

  • This is close, but it still requires prefix.
    – user716468
    Feb 22, 2013 at 23:26

My plugin zsh-autocomplete can now do this:

enter image description here

As you can see, after pressing ^R, it allows you to type any part of the word you're looking for.


In bash(1) (this comes originally from the C shell) you can repeat previous commands using ! (as long as they are in the history). So, e.g.:

enter code here

frob --vigorous extremely/super/cali/fragi/listic/file/name
xemacs !frob:$       # Edit last argument to last frob command
!frob                # Repeat last frobbing
fiddle !frob:1-$     # Fiddle, use arguments 1 to last of !frob

The syntax takes some getting used to, and (due to the luxury of readline) I use it rarely, but it is quite handy. It is (or used to be) somehwat fragile, in that for example it worked unpredictably in scripts (but there I'd very much make thrice sure I'm running the right command, so...).


Play with ALT-. , from bash manpage:

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

You must log in to answer this question.

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