19 Jul 2017 DBHist: bash history in sqlite sqlite, bash, terminal.app, and term

I spend a lot of time in terminals, as most of you. I never been more productive than when I started to use dbhist .

You probably know several ways how you can search bash history. My ways were Control-R or history | grep foo . Both have limitations, both has issues. With grep it always a hassle for me to write POSIX compatible regexp, like it always gets me, it should be [[:digit:]] , not \d . And don’t want to mention how often bash corrupts or unexpectedly overrides history file, which I use one for all sessions.

Also remember the time when I kept huge cheatsheets with various commands. The main problems with that approach was - I needed another cheatsheet to remember that I had one, and they become outdated very quickly.

I am not the first one, who decided to use sqlite for storing bash history. I have seen some mentionin on web. I have found some very good implementations. But could not find anything I liked. Some were written in python (additional dependencies) or zsh (nice shell, used it for a while, but decided to stay with default), had dependencies on package managers, or other libraries (bash scripts). I really wanted something simple, something I could just put in my dotfiles repo and modify it when I need it.

So at the end I wrote my own implementation. You can find it on gist (also embedded below).

It has only one requirement, set HISTTIMEFORMAT="%s " . That tells bash to store commands in history with time in Unixtime format. After that source the file

$ source dbhist.sh

I have verified that it works with Bash 3.2 (default on macOS) and 4.x.

To get help

$ dbhist --help

To start searching your history (it does not import your current bash history, so don’t be surprised, that it does not give you much at the beginning) just type

$ dbhist git

That only shows results for current session, if you want to search across all sessions

$ dbhist --all 'git status' 8804 | 2017-07-19 07:52:49 | 373 | git status

I have typed git status 373 types from the time I started to use dbhist . It shows the last time I typed it in second column. The first column is just an incremental id of the last saved command.

You can see how often you pushed current git repository if you invoke this command from its folder

dbhist --all --under 'git push' 8770 | 2017-07-17 20:19:04 | 12 | git push

In this case --under means to show all commands executed in current folder or subfolder. You can also find commands executed only in current folder with --here . For example show me last 5 commands executed in home folder

$ dbhist --here --all --limit 5 8843 | 2017-07-19 15:05:48 | 2 | cd 8814 | 2017-07-19 14:28:53 | 6 | cd .dotfiles 8741 | 2017-07-17 11:55:44 | 17 | cd dev 8740 | 2017-07-17 11:55:42 | 29 | ls 8651 | 2017-07-17 11:55:42 | 9 | brew upgrade

And the most useful feature. For example I remember that to invalidate cloudfront distribution I use aws tool. And I remember that command has a word invalidation , so

$ dbhist --all --starts 'aws%invalidation' 8531 | 2017-07-13 19:26:43 | 7 | aws cloudfront create-invalidation --distribution-id ERCTXXXKGZXX4 --paths "/*" 6269 | 2017-06-30 13:19:06 | 3 | aws cloudfront create-invalidation help 6268 | 2017-06-30 13:18:56 | 1 | aws cloudfront get-invalidation --distribution-id ERCTXXXGZSH4 --id I2XXXXKXU3991 6266 | 2017-06-30 13:17:40 | 1 | aws cloudfront create-invalidation --distribution-id ERCTXXXKGZXX4 6262 | 2017-06-30 12:48:25 | 1 | aws cloudfront list-invalidations --distribution-id ERCTZXXXGZXX4

Where % is a SQL syntax, similar to globbing pattern Wildcards. Parameter --starts tells to search only when command starts from the aws... , by default dbhist searches with pattern %query% , which means that query can be anywhere.

Hope you can also find it useful.

Below is the listing of dbhist.sh , you can also find it on gist.github.com.