As the title, type M-C-y, can use it, but what if i want to use the argument to recall the nth argument in the previous command...Thanks for answering...

Dani AI

Generated

A little extra context and some handy alternatives to ’s reply so the feature is easier to use and to troubleshoot.

Readline provides both a general "yank nth argument" widget and a convenience "yank last argument" widget; these pull words from the previous history entry for interactive editing (so they’re Readline features, not shell-script constructs). The widgets accept numeric arguments (including negative ones) and use history word-designators to extract the requested word. (gnu.org)

If the Meta/Alt key behavior is awkward, remember the Escape key can be used as a Meta prefix (press Escape then the rest of the sequence). For a quick interactive alternative that many find easiest, the “insert-last-argument” widget (commonly invoked with Alt + .) pastes the last word of the previous command at the cursor and repeated presses walk back through prior commands’ last words. For non-interactive uses or scripts, use the shell/history mechanisms instead (see next paragraph). (bashcookbook.com)

Useful alternatives and quick examples:

  • Use the special shell variable $_ to refer to the last argument from the previous command in scripts or commands.
  • Use history-expansion word designators when you want the shell to expand before execution, e.g. !!:$ or !!:2 to grab specific words from the previous command (these are expanded when the line is accepted).
  • To see what Readline currently offers, run:
    bind -P | grep -i yank
    bind -q yank-nth-arg

    These help you confirm bindings and function names. (gnu.org)

To make a custom key easier to use, add a binding to ~/.inputrc (Readline init) and reload it. Example format:

"\C-x\C-y": yank-nth-arg

Then reload with bind -f ~/.inputrc or the readline re-read key. Use bind and the Readline init-file docs to fine-tune bindings and keymaps. (gnu.org)

Overall: the builtin widgets are reliable for interactive editing; use $_ or history-expansion when you need programmatic or scriptable access.

Recommended Answers

All 2 Replies

Hi there. I just figured this out after reading your thread here, so in case you haven't found it yet, the answer is this:

Type M-(arg)-C-y, so if you wanted to recall the 3rd argument of the last command (note that arg numbering starts at 0) you would hold CTRL-META, type 2, and type y.

Also, if you are on a Mac, you will have to make sure your Terminal is set to use Option as a meta key (Preferences > Settings > Keyboard).

Cheers,
-elliott-

Thanks for your answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.