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.
You can get to edit mode by hitting M-^ (option-shift-6 on a mac).
Type this:
And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.
^p to get the last typed command in unix/solaris
Actually, you can just append
:p
to the command to print it without actually running it. For example:Will print out
ls -la
as the previous command without running it, and you can just press ↑ (up) to find it and edit it.You can also do
to print out the 123rd command as your previous command.
You can also try
fc
command to edit the command in the history.WIKI says,
Apart from reverse-incremental search(Ctrl+R), we have some more bash shortcuts:
From
man bash
:You may wan to try "suggest box"-like history https://github.com/dvorka/hstr - it reads Bash history and allows for quick navigation.
To get the last command simply type hh, navigate to the command and use right arrow to get it on command line (where you can edit it and/or add comment).
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