ale ale - 10 days ago 5
Linux Question

Using bash history to get a previous command, copy it and then 'run' it but with the command commented

Just a question to improve my

bash
skills. I always do this:

$ history | grep some_long_command

...
...
123 some_long_command1.........
124 some_long_command2.........
...


I can then run the command the command I found by doing:

!123


However, I often want to do this:

some_long_command1foobar


I.e. change the command before I run it. Can you use bash to run this command instead:

#some_long_command1


so it gets commented.

Then I don't have to use my mouse to highlight the command, edit it and then run it (I can just use the keyboard - faster).

I suppose I could write a script to do it but there might already be functionality built in somewhere....?

Thank you.

Answer

I'd suggest instead of using the history command, you use ctrl+r and start typing that command. When you press an arrow key as if to go to modify it, it will drop out of autocomplete recognition, and will let you edit before running.

UPDATE: also, if you want to cycle through the different commands that contain the string you just typed, keep on pressing ctrl+r