0

How to insert an entire command from history into the current command in Bash?

I've done some research and identified two GNU Readline key-bindings that would insert the first and last words of the previous command. Unfortunately, I don't know how to supply an argument n to these key-binding (how-to?), and even if I knew, it would not be feasible for inserting long commands from history.

Ctrl-Alt-y

Insert the first argument to the previous command. With an argument n, insert the nth word from the previous command.

Alt-.
Alt-_

Insert the last argument to the previous command. With an argument n, insert the nth word from the previous command.

Does GNU Readline support inserting entire command from history into current command?

DISCLAIMER: I'm not looking for Bash command substitution.

0

Enter the desired history expansion (say !foo or !39) and expand it:

history-expand-line (M-^)
Perform history expansion on the current line.

Meta would be Alt, IIRC.

| improve this answer | |
0

You can create a binding yourself. You only have to decide what key to bind it to. Example with Ctrl-T

insert_last_line(){
     local l=$READLINE_LINE p=READLINE_POINT h=$(fc -nl -0)
     READLINE_LINE=${l:0:p}${h#* }${l:p}
}
bind -x '"\C-T": insert_last_line'

Making it insert previous entries when repeated is left as an exercise to the reader ;-)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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