4

I'm aware that you can recall the last argument of the previous command with !$ and also cycle through previous arguments with ALT + . but can you recall the last argument used for the current command that is being used?

e.g,

root@chip:/# echo peckahs
root@chip:/# *_stuffthatdoesntmatter_*
root@chip:/# *_stuffthatdoesntmatter_*
root@chip:/# *_stuffthatdoesntmatter_*
root@chip:/# echo peckahs < used key shortcut to recall 'peckahs' the last used argument for echo
  • In short: I don't think so. – jimmij Feb 06 '15 at 16:46
  • if the question isn't restricted too keybinds, echo foo{,} would print foo twice. – llua Feb 06 '15 at 17:54
  • @llua I think Hauke Laging's edit to my question confused you, the last command would be echo peckhas not echo peckahs peckahs After I type echo I want to use a shortcut to insert the last argument used for echo which would result in echo peckahs – regularjoe Feb 06 '15 at 18:03
  • I believe you would need to write a completion function that inspected history. Vague workarounds are to use reverse-history-search (^R), or dynamic-complete-history (Esc ^I) which completes previously used words, but independent of the command. – mr.spuratic Feb 06 '15 at 18:19

3 Answers3

3

With bash you can do nearly what you're asking for like this:

echo !echo:$

So when you do

echo This is fun
ls
echo !echo:$

the last line outputs fun. * instead of $ produces all the arguments to the matching command; so

echo This is fun
ls
echo !echo:*

outputs This is fun again; but you might as well just do

!echo

in this case.

This isn't limited to repeating a command's arguments with the same command:

printf !echo:*

See the bash reference manual for details. You could even combine this with the histverify shell option to give you a chance to check the command before it's executed, which gets you close to keyboard-interactive history-based completion.

History expansions can be used anywhere; so for example

lastechoargs="!echo:*"

stored all the arguments to the last echo command in the lastechoargs variable.

This works for complete commands too; say for example you've worked out a complex git command, and you want to save it in a file:

echo !git > mygitcommand

Or you want to archive some directories, but you decide to delete a couple of files first:

ls dir1 dir2
rm dir2/somefile
tar cpzf archive.tar.gz !ls:*
Stephen Kitt
  • 434,908
0

If your bash in emacs-mode you can use Ctrl+w (delete last word) then Ctrl+u (input deleted string)

Costas
  • 14,916
0

Yes. In any POSIX shell you can do...

set -o vi
^[[num]-#^[[num]_

...where ^[ represents the ESC key and [num] represents an optional parameter for nth history command counting backwards from the current one and nth argument in the previous command counting backwards from the last respectively. Depending on your keyboard config you may be able to use CTRL plus the combination key instead.

The sequence calls up the numbered command, prepends a # comment to it, and stores it back in the shell history as the most previous command. Then the ^[_ shortcut calls up the last arg in the last command.

bash also saves the last arg by default in the shell parameter $_ so after doing the first part you could just expand the last arg in that way as well.

mikeserv
  • 58,310