Live data from Hacker News

Better Bash History (2012)

sanctum.geek.nz

51–60 of 85 posts

Re: Better Bash History (2012)

#51

I set up my bash config to store history separately for each working directory (ie separate history files that I store under a $HOME/.bash_history_d directory) – so as I cd into a project directory, the history is populated with the commands that I've run within that directory . I find this super-helpful to remind me of the context for projects – cd in, then up-arrow to whatever that command is I usually run to launc…

Could you share details on how did you setup this. This sounds super useful

Re: Better Bash History (2012)

#52
post #30

I have a global bash history, having set up bash/zsh to [immediately share without any annoying race conditions.][2] I use FZF mapped to CTRL+R to search through my history. The unique thing is that I use a [python script (in my dotfiles)][1] to clean the history in a more advanced way than is possible with HISTIGNORE. The script removes duplicates, leaving the most recent copy and removes lines matching several rege…

I was recently planning to write something just like this. Thanks for sharing.

Re: Better Bash History (2012)

#53

I set up my bash config to store history separately for each working directory (ie separate history files that I store under a $HOME/.bash_history_d directory) – so as I cd into a project directory, the history is populated with the commands that I've run within that directory . I find this super-helpful to remind me of the context for projects – cd in, then up-arrow to whatever that command is I usually run to launc…

I set PROMPT_CMD to the following function:

    function log_history {
        echo "`date '+%Y-%m-%d %H:%M:%S'` $HOSTNAME:$$ $PWD ($?) `history 1`" >> $MYHISTFILE
    }
which records a timestamp, host name, shell pid, working directory and exit status for each command.

Re: Better Bash History (2012)

#54
post #25
post #23

Earlier quoted context omitted.

I use zsh, and I have a thing in my profile that skips writing any `cd` command to history. Instead, it will rewrite the command using the absolute path of the directory I `cd`'d into and write that to history. Thus, all of the `cd` commands that get written to my history use absolute paths and are re-usable no matter where I am. It would be cool if this sort of functionality were generalizable to all commands where…

You might enjoy checking out 'fasd', which, among other things, creates an alias ('zz') with autocomplete functionality for changing to frequently-used directories. https://github.com/clvv/fasd

Or just github.com/rupa/z

Re: Better Bash History (2012)

#55

To echo what everyone else is saying, get FZF. Most of these linux productivity things are just some dumb pet project that solves a specific problem that a specific person finds annoying. FZF is not this - it makes something you do hundreds of times a day (history and file opening on zsh, bash, fish, whatever) indisputably better. If you find yourself sitting there tapping ctrl+r multiple times like an idiot for some…

Yeah, these kinds of hacks usually end up with unmaintainable mess when cumulated. Just use fzf.

Re: Better Bash History (2012)

#57
post #44

I set up my bash config to store history separately for each working directory (ie separate history files that I store under a $HOME/.bash_history_d directory) – so as I cd into a project directory, the history is populated with the commands that I've run within that directory . I find this super-helpful to remind me of the context for projects – cd in, then up-arrow to whatever that command is I usually run to launc…

zsh-histdb stores history inside a sqlite database instead of a simple text file. The good thing is that it includes context: current directory, session, date. I use it together with ssh-suggestions which can use SQL queries to find the most relevant command. You can program rules such as: use the most frequent command in that session, if you none do it for the current directory in the last 6 months, if not, search g…

isn't that totally opposite of the *nix philosophy of everything is a text file?

Re: Better Bash History (2012)

#58

Earlier quoted context omitted.

This is lovely, and I should integrate it with the chronology based approach I've been using. I keep an infinite log by appending a timestamp to the beginning of the lines written to my history file, then merging this into a growing log with an hourly cronjob that runs sort -n | uniq on the file and the new version of the shell history. This way the shell history file is just a buffer. It doesn't get too big to cause…

But doesn’t that mess up the order of reverse-i-search?

No because every line has a timestamp and the sort is done comparing numerical value, but still I question the use of uniq, unless it's invoked with the --skip-fields option.

Re: Better Bash History (2012)

#59
post #30

I have a global bash history, having set up bash/zsh to [immediately share without any annoying race conditions.][2] I use FZF mapped to CTRL+R to search through my history. The unique thing is that I use a [python script (in my dotfiles)][1] to clean the history in a more advanced way than is possible with HISTIGNORE. The script removes duplicates, leaving the most recent copy and removes lines matching several rege…

If one is only interested to remove duplicates (your script does more) I've found this one liner in a StackExchange post: awk '!x[$0]++' ~/.zsh_history
Post reply on HN