Hacker News new | ask | show | jobs
by larkery 3361 days ago
For those on zsh I have something similar [1-2]. It hooks to zshaddhistory and stores the command, running time, CWD, hostname and exit status in a sqlite database, and provides a simple query command.

With a git merge driver [3] the history database can be kept in source control and shared between hosts.

Queries look a bit like

    $ histdb blah
    time   ses  dir         cmd
    09/02  24   ~           ogr2ogr temp/blah.shp 'WFS:http://environment.data.gov.uk/ds/wfs?SERVICE=WFS&INTERFACE=ENVIRONMENTWFS&VERSION=1.0.0&LC=3000000000000000000000000000000'
    09/02  24   ~/temp      cd blah.shp
    15/02  146  ~/.emacs.d  git commit -am "blah"
    22/02  175  ~           test="asdf/blah.shp"
    09:48  743  ~/.histdb   hist blah
The sess column here is a unique (per-host) session number which means it can recreate any transcript with a suitable query; if I ran histdb -s 24 it would produce the whole session containing the top two results above, including directory history.

[1] https://github.com/larkery/zsh/blob/master/sqlite-history.zs... [2] https://github.com/larkery/zsh/blob/master/self-insert-overr... [3] https://github.com/larkery/zsh/blob/master/histdb-merge.zsh

1 comments

Looks good. You should separate this into another repo with a README. I use zgen [1], a package manager with ZSH which lets you add libraries via github repos. That way I can keep it up-to-date without manually checking in on the project.

Also your [3] link is broken.

https://github.com/tarjoilija/zgen

Edit: any way to import the existing .zsh_history?

It appears I am outside the edit window, but [3] should be https://github.com/larkery/zsh/blob/master/histdb-merge (no .zsh). Thanks for pointing that out.

I don't use a zsh package manager and don't really want to learn one, but if you can tell me the minimal glue I need to add to make these files into a package I will happily duplicate them into another repository and keep them up to date there. Ed.: https://github.com/larkery/zsh-histdb

I haven't done anything to import the history file because some of the information is missing so it would be a bit untidy. You could do something like

    history 0 | while read -r num line; do
        zshaddhistory "$line"
    done
This will make bogus entries where the history includes newlines; you could probably use a loop on history number instead, or do something in the while loop to accumulate split lines by looking at "$num" and seeing if it goes up.