Live data from Hacker News

Better Bash History (2012)

sanctum.geek.nz

41–50 of 85 posts

Re: Better Bash History (2012)

#42

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…

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?

Re: Better Bash History (2012)

#43

PROMPT_COMMAND is probably already used for something else by your distro. HISTFILESIZE and HISTSIZE can be set to -1 for unlimited. Read man bash.

HISTFILESIZE and HISTSIZE don't solve the abysmal bash history file handling when you have more than one bash open.

I would also like to know which distro clobbers user settable env variables.

Re: Better Bash History (2012)

#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 globally.

Re: Better Bash History (2012)

#45

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…

Yup, I tried the context aware history for couple of days and it got annoying pretty fast. I have everything in one file which I can just FZF reverse search and bam. No need for infinite up arrows.

Re: Better Bash History (2012)

#46

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…

Thank you for this. It always amazes me when I come across something that would be so useful to me, and yet so obvious in hindsight.

Re: Better Bash History (2012)

#47

Because this comes up every so often, I'll post this link again: I wrote a little widget that stores your bash history in a sqlite db. I haven't touched it in a while because I now rely on fzf and this doesn't integrate with it, though maybe I could make it do... https://github.com/thenewwazoo/bash-history-sqlite

This is cool, somebody recently suggested that I add history to sqlite in Oil [1]. I looked at your code but I didn't see how it integrates with bash?

Most people seem to be using the PROMPT_COMMAND hook for stuff like this -- are you using something else, or did I miss it?

https://github.com/oilshell/oil/issues/320

I will add PROMPT_COMMAND to Oil, but I'm curious if there is something else I should add to support end-user customization.

One thing that might be interesting is to support a hook for the ! syntax, so the sqlite DB could be searched. I think that would solve some of the problems you mention in the code's comments, and maybe the fzf issue. (Although that literally just occurred to me, I don't promise to do this :) )

I haven't looked at how fzf works exactly. But if you want to chat about it please chime in on Github :)

[1] Latest status: Success With the Interactive Shell http://www.oilshell.org/blog/2019/02/05.html . Oil runs a bunch of interactive programs like bash-completion, virtualenv, and git-prompt!

Re: Better Bash History (2012)

#48
post #28

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've done the same thing, and I've found it super helpful. My particular configuration is at https://github.com/YenTheFirst/dotfiles/blob/master/.bash_cd... 90% of the time, the bash history is super relevant to the current context. A small amount of the time, I remember that I issued a command, but not in which directory - in these cases, I can just search the history directory, and get both the context of what the…

Thanks, reading over that code was useful. In particular I realized that my Oil project (a bash-compatible shell [1]) doesn't support PROMPT_COMMAND, which tons of people appear to be using.

https://github.com/oilshell/oil/issues/320

It seems like a weird hack but apparently it works for tons of things!

[1] Latest status: Success With the Interactive Shell http://www.oilshell.org/blog/2019/02/05.html . Oil runs a bunch of interactive programs like bash-completion, virtualenv, and git-prompt.

Re: Better Bash History (2012)

#49

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…

> Anyone else done something similar?

I'm not, but now that you mention this, I think I will. I already use direnv all over the place, and this would be a pretty natural extension. Thanks!

Re: Better Bash History (2012)

#50
post #20
post #2

highly recommended for fellow ctrl-r addicts: https://github.com/junegunn/fzf

I also recommend FZF. Really very handy for doing CTRL-R searches. I also use it for doing interactive git rebases. I have an alias for doing a fuzzy rebase interactive (frbi): frbi = !git rebase -i $(git log --pretty=oneline --color=always | fzf --ansi | cut -d ' ' -f1)^

This is a great idea, thanks for it! I've done similar things with using fzf to git add, git diff, and so on, but I hadn't thought of using it to pick a commit to rebase from.

I made a few changes so I figured I'd post them back here in case you're interested:

    pick-commit = # equivalent to your git log... cut...
    rif = "!f() { rev=$(git pick-commit); [[ $rev ]] && git ri $rev~; }; f"
"rif" = "rebase interactive fzf/fuzzyfind". The function and test just makes it exit cleanly if you escape out of the filter instead of giving "fatal: invalid upstream '~'" (and I changed it from ^ to ~ because I think ~ is usually what one should use over ^, but I'm willing to be corrected). And obviously I already have 'ri' aliased to 'rebase -i'.

While I'm here, here are my similar git aliases for adding, diffing, etc. files using fzf:

    pick-status-files = "!f() { git status -z | xargs -0n1 | cut -c4- | fzi --print0 | xargs -0to git "$@"; }; f"
    af = !git pick-status-files add
    # and so on for diff, difftool, checkout, etc.
Oh, and 'fzi' (fuzzy-inline) is a small shell script that just calls:

    fzf --height 30% --reverse --multi "$@"
Post reply on HN