Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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....?

like image 647
ale Avatar asked Jun 12 '12 16:06

ale


1 Answers

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

like image 98
Miquel Avatar answered Oct 24 '22 07:10

Miquel