Live data from Hacker News

Better Bash History (2012)

sanctum.geek.nz

31–40 of 85 posts

Re: Better Bash History (2012)

#31
post #9

Bash's broken history is what finally got me to switch to Zsh. The history behavior you want is: 1. each line of history is appended incrementally, but 2. each shell session only has access to its own history unless you specifically reload the history into your session. Every combination of history commands in bash didn't do the right thing[1], but Zsh's works as desired. [1] eg. putting 'history -a' in PROMPT_COMMAN…

`setopt inc_append_history_time` is super useful for Zsh users. This gives you some sharing of histories between shells.

Re: Better Bash History (2012)

#32
post #29

I do: promptFunc() { # right before prompting for the # next command, save the previous # command in a file. echo "$(date +%Y-%m-%d--%H-%M-%S) \ $(hostname) $PWD $(history 1)" \ >> ~/.full_history } PROMPT_COMMAND=promptFunc This is in addition to standard bash history, and is much more reliable. Being able to see not only what command I ran but what directory I was in and when I ran it is very useful. (I wrote about…

I do something very similar (just saved in files by month) and it has saved my bacon a few times. I was recently asked to re-run a process that I ran years ago. It would have taken me hours to re-discover how to run the process but instead took a few minutes after grepping the commands.

Every developer should do this.

Re: Better Bash History (2012)

#33

Earlier quoted context omitted.

I do something similar, but instead of current directory I have a notion of context that is carried by an environment variable set in a GNU screen process (and therefore inherited by its children) which drives logic in my .bashrc. https://github.com/dlthomas/config-files

Fish does this by default (and much more as well).

Could you elaborate? I can read docs, but it's not clear to me what correspondences you have in mind.

Specifically, I'm not sure what "this" is that fish does, as I described interaction of multiple pieces of software and I don't think fish includes a terminal multiplexer?

Re: Better Bash History (2012)

#34
post #23

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 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…

That seems like it would be tricky for it not to screw something up.

Like, for example, would it handle this properly?

$ (cd /source;tar -cf - .) | (cd /dest;tar -xf -)

Re: Better Bash History (2012)

#35

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 corruption or other issues with reverse search on the command line. I just grep my way through the big history file to figure out what I was doing and when.

Re: Better Bash History (2012)

#37

Earlier quoted context omitted.

Fish does this by default (and much more as well).

Could you elaborate? I can read docs, but it's not clear to me what correspondences you have in mind. Specifically, I'm not sure what "this" is that fish does, as I described interaction of multiple pieces of software and I don't think fish includes a terminal multiplexer?

autocompletion in fish takes into account the directory where you are at, so it will autocomplete commands ran in the same directory before other commands.

Re: Better Bash History (2012)

#38
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 thing that may or may not be there, do your stubborn beardy ass a favour and install fzf.

You can implement some or all of the tips in this HN post, it'll make fzf results better.

Re: Better Bash History (2012)

#39

Earlier quoted context omitted.

Could you elaborate? I can read docs, but it's not clear to me what correspondences you have in mind. Specifically, I'm not sure what "this" is that fish does, as I described interaction of multiple pieces of software and I don't think fish includes a terminal multiplexer?

autocompletion in fish takes into account the directory where you are at, so it will autocomplete commands ran in the same directory before other commands.

If that's what's being referred to, it sounds like dual_base meant to reply to aaronharnly? As mentioned, my setup isn't based on working directory.

Re: Better Bash History (2012)

#40
that's exactly what my setting is for history, nice post. The only difference is that I unset HISTCONTROL, don't recall why I did that.

    shopt -s histappend is the default already I believe.
    shopt -s cmdhist is also the default now.
    HISTSIZE=-1 will be unlimited, which is fine for today's computers.
Post reply on HN