2

Ctrl-R in bash and zsh is very useful. So is the !! alias - which expands to the last command. I often find myself doing things like: cat file | !!, before pressing tab to tweak the command. I would like to search my history incrementally when using !!, but ctrl-R only seems to work at an empty prompt.

Is there a way to insert historic commands somewhere other than the beginning of a line?

Approaches tried:

I tried $(fc -nl | fzf) TAB but this quoted the last command. I could use something like a zle plugin (like I used in this) but it feels like overkill...

1 Answer 1

1

I usually first search back with reverse-search-history (ctrl-r) to find the wanted line, then use beginning-of-line (ctrl-a) and kill-line (ctrl-k) to copy the line into the kill ring, then interrupt with ctrl-C so as not to disturb the history. I can then type my command, cat file |, and yank (ctrl-y) the last kill at the appropriate point.

If I've already started typing the command, I go to the beginning of the line, add a #, and press enter to put it in the history, then do the search back, and, after the interrupt, recover the commented line from the history.

2
  • You can just use ^U to copy the whole line. But why not just ^A and insert <file there? Commented Jul 25, 2023 at 12:29
  • The cat file | is just the example from the OP. Presumably, something more complicated might be wanted.
    – meuh
    Commented Jul 25, 2023 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .