-1

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.

Shuzheng
  • 4,411

2 Answers2

1

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.

muru
  • 72,889
1

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 ;-)