1

In bash, I know how to

history |grep foo

which will give me a list of numbered commands from history containing foo. If I want to run one of those commands with a different argument, I can copy paste using the mouse. I don't want to repeat an exact command. I want to load it at the prompt and change something, then execute it. Is there a way to do so? Up arrow doesn't always work for me.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 3
    Is CTRL+r and then using the left/right arrow keys to edit parameters not an acceptable workflow? – kemotep Jul 09 '19 at 14:48

1 Answers1

3

If you’re using Bash, you can enable histverify and then recall commands with ! followed by the command’s number in the history:

shopt -s histverify

Another interesting approach is to use fc:

fc 200

will load the 200th command in your history into your editor. You can then edit the command as you please, and it will be executed when you exit your editor.

Other shells such as Zsh have similar features.

See the chapter on history interaction in the Bash manual for details.

Stephen Kitt
  • 434,908