(reverse-i-search)
(Ctrl+R) replacement.
There are many tools out there to enhance history and searchability. To name a few I found and were inspired by (in alphabetical order):
But all those, and frankly, the default history
lack (in my view) is good ergonomics to find and use part of a previously executed command.
Some times, I want to do something like:
I want to find what program is using up port 4000 and 4001.
And my typical workflow goes like this:
- Search "linux netstat program port".
- Open the same StackOverflow question that has the snippet:
netstat -nlp | grep 9000
- Copy that command and execute.
- Up arrow (↑) to re-type the previous command.
Ctrl + w
to delete the last9000
.- Type in
4000
- Realize I'm not logged as super user
- Up arrow (↑) to re-type the previous command.
Home
to go the the start of the line.- Type in
sudo
. Enter
. Success I found the program on port4000
.- Up arrow (↑) to re-type the previous command.
Ctrl + w
to delete the last4000
.- Type in
4001
. Enter
. Success I found the program on port4001
.
Many might know that step 9 and 10 could be replace by typing: sudo !!
, as !!
get expanded to the to the previous command executed.
Some might even know that steps 12, 13 and 14 can be done by doing ^4000^4001^
.
From the manual of history
:
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to
``!!:s/string1/string2/'' (see Modifiers below).)
But, most notable for me is that steps 4, 5 and 6 can be replaces with: !:0- 4000
. This can be read as: “Re-run the last command (!
) but take the arguments from the start (0
) to the last word (-
) and then concatenate 4000
”.
Long gone are the days where we have terminals and no arrow keys. This makes no sense why there is no TUI-like utility. Well… there was no TUI-like utility until now!!
The recommended way of using happy-history
is to do some bash/zsh magic to bind the execution. But it is not necessary.
Once you are in happy-history
, you can do exactly the same as you were doing in reverse-i-search
. To reiterate, this is a drop-in replacement of that functionality. So sudo !!
, ^4000^4001^
, et al. all keep working
One in happy-history
you can type as you did in reverse-i-search
.
You can scroll though the results with Ctrl + R
or the vertical arrow keys (↑/↓).
Pressing horizontal arrows keys (→/←) focuses on the command. There you can move about and edit in place.
To go back to searching, press Ctrl + R
or the vertical arrow keys (↑/↓) again.
Default Key bind | Action |
---|---|
Ctrl + R | Search the previous occurrence of the searched term |
Esc | Exit |
Enter | Execute the command |
↑/↓ | Scroll though occurrences of the searched term. If no term was searched, it scrolls though previous commands*. |
→/←/Home/End | Switch focus to the command editor, and move the cursor around |
Ctrl + E | Open an editor with the current command. The resulting command will be executed. sort of like fc |
- How to install in bash (
bind
? vi mode / emacs mode) - How to install in zsh (zsh plugin?)
- Should we deal with pipes as a different event?
- Can
hh
type the command without executing it?
Check Architecture.md for a more in-depth explanation of the source code and the development.