This question already has an answer here:

In bash, what is the shortcut to recall the last command without any arguments?

That seems to be the one shortcut that is missing form the various guides I have found, such as https://linuxconfig.org/linux-command-line-bash-shell-shortcuts

I would like the last used command to appear on the command line with the cursor at the end of the line so I can type the new argument I wish to supply.

Furthermore, I would like to achieve this with a single shortcut key combination along the lines of CTRL+P

!:0 does not work because it actually executes the command without giving me an opportunity to enter new argument(s).

Meta+Ctrl+e fails to produce any results in bash for me. However, the following indicates it should work, assuming \e refers to Alt

bind -P | grep shell-expand-line
shell-expand-line can be found on "\e\C-e".

I'm running Arch.

marked as duplicate by muru, jasonwryan, Romeo Ninov, Toby Speight, Dmitry Grigoryev Mar 13 '18 at 9:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @muru that question is slightly different and the accepted answer doesn't do what I am trying to achieve. I clarified my question to make that more clear. – MountainX Mar 13 '18 at 7:12
  • @muru you linked to a comment that suggests using Meta-Ctrl-e; (Meta is usually Alt). I tried that (trying both Alt keys) and the command does not do anything in bash for me. I'm running Arch Linux (current). – MountainX Mar 13 '18 at 7:32
  • @muru I see how it works now. !!:0 then Ctrl-Alt-E does work, but that is a lot of typing, which is what I'm trying to avoid. – MountainX Mar 13 '18 at 8:03
  • Just !!:0 will get replaced with the command when it is executed. I don't even bother to expand it when using. – muru Mar 13 '18 at 8:04

With bash or zsh in emacs mode, you can use Alt+0Alt+_. That is hold the Alt key and press 0 and underscore in sequence (which on a qwerty keyboard are next to each other, though you need Shift for underscore).

It expands to the first syntactic word of the previous command line. That may not be the last command. For instance, if the last command line was:

$(echo echo foo) bar

It would expand to $(echo echo foo), not echo.

If you want it to be a single key, you can use bind in bash or bindkey in zsh to bind that sequence to a key.

For instance, to bind to F12:

  • bash:

    bind "\"$(tput kf12)"'": "\e0\e_"'
  • zsh:

    bindkey -s $terminfo[kf12] '\e0\e_'
  • ... which is similar to the other answer of the linked post: unix.stackexchange.com/a/179861/70524 (but _ instead of .) – muru Mar 13 '18 at 7:45
  • @muru, ah yes. I didn't see the comment or linked answer. Then it's a dup or am I missing something. – Stéphane Chazelas Mar 13 '18 at 7:48
  • so far I have been unable to get bind to work. Can you give an example of how to bind CTRL-0 to this function? – MountainX Mar 13 '18 at 7:53
  • @MountainX, see edit. – Stéphane Chazelas Mar 13 '18 at 7:56
  • @MountainX doesn't look like Ctrl-0 will work: superuser.com/a/27318/334516 – muru Mar 13 '18 at 7:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.