Just a question to improve my bash
skills. I always do this:
$ history | grep some_long_command
...
...
123 some_long_command1.........
124 some_l
You can get to edit mode by hitting M-^ (option-shift-6 on a mac).
Type this:
!123M-^
And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.
!123:gs/old/new/
Will run command 123 replacing the string 'old' with the string 'new'.
^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:
$ ls -la
$ !!:p
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
!123:p
to print out the 123rd command as your previous command.
Instead of using the history
command, bind history-search-backward
/history-search-forward
to key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrc
file:
"<key code>": history-search-backward
"<key code>": history-search-forward
To get <key code>
, type Ctrl-V <key>
in the shell, and replace the starting ^[
with \e
in whatever was output.
After this is set up, you can just type some
and press PgUp to get some_long_command
. If you need some_long_command with_some_arg
but there is a similar command some_long_command with_some_other_arg
later in the history, you can cycle through until you reach it by typing some
and then hitting PgUp repeatedly, or you can type some
, hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R
.
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).