How do I search shell command history with a text editor?
I know that Ctrl+R let's you search command history, but it's a little primitive. Is there a way to export all my command history (not just the current terminal session, but the full history) to a text file? I could then use a text editor to search it comfortably. Or if the history file already exists, where is it?
From man bash
:
HISTFILE
The name of the file in which command history is saved.
The default value is ~/.bash_history.
If unset, the command history is not saved when a shell exits.
So, the variable HISTFILE
will contain the filename where the history will be saved.
$ echo "$HISTFILE"
/home/user/.bash_history
You can now search for the pattern:
$ grep "vim" "$HISTFILE"
vim foo.text
vim bar.text
vim file.txt
As @Dennis has pointed out, if you want you can run history -a
to append the command history of the running session to the $HISTFILE
file. Basically the commands will be automatically appended once you close a session, history -a
will do the same thing right at that instant.
Run help history
to get more idea on the history
builtin itself.
Try this:
history > output.txt
less output.txt
Then search by typing /
+ searchterm
The bash history is saved in your home directory ~/.bash_history
.
Basically Ctrl + R searches from this file.