Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Whoever can point me at a robust method to save/restore bash histories of a tmux session would save my life!!! [And yes i use tmux inside tmux]


About a year ago I went further into this rabbit hole and right now I think I am on a re-iteration of a system I have not changed in about 4 months. It came to me after watching too much sci-fi that had clicky-clicky-typie-typie instant recall.

It occurred to me that every hack around history just nibbles at the edges probably because all of them are bolted on a design when the systems were slow. Systems now are fast and have good connectivity ( at least to themselves ) hence:

"All logins across all systems that have the same security domain share single common command history."

I use bash but it should be adoptable to anything. Here's how it works:

1. There's a database. I use redis because it is fast and has SETNX. [I may switch this to a redis queue to be able to archive commands as well.]

2. Using PROMPT_COMMAND variable I execute a function that pulls the last executed command using "history 1" and pushes it into the database. SETNX ensures that only a command that has not been seen before is stored. In the same transaction I trim the number of commands stored in the database to 5000.

3. Ctrl-R is bound to fetching last 5000 commands from the database, piping it into "peco" which I use with the selection line on a top ("fzf" does not support the top line so I don't use it here. "peco" does not support certain header skips/treatments, so i use "fzf" in other places )

4. On a failure to push into the database, system does nothing as the data is in a local history.

5. On a failure to pull from the database, system feeds the result of the local history into the "peco".

This system gives me ability to access the same command line history across my laptops, workstation, VMs, random Linux devices that i have running at home. Our ops-team uses it across all the monitoring workstations


I'm using a variant of this: https://spin.atomicobject.com/2016/05/28/log-bash-history/

Logging the entire bash history in files sorted by day: export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'

I haven't overloaded ctrl-r yet to search in these files but that would be a possibility.


Histdb is amazing, but requires zsh. I have one continuous history. I'm working on figuring out multi-host history for One True History.

https://github.com/larkery/zsh-histdb

I haven't tried it, but the keywords you are looking for is bash history db/sqlite. E.g.:

https://github.com/thenewwazoo/bash-history-sqlite/blob/mast...


I use this to have all shells share a history concurrently - which sometimes is confusing if you press "↑" and have an unexpected (possibly dangerous command) but I'm used to it now:

  shopt -s histappend
  PROMPT_COMMAND="history -a; history -c; history -r"
See more about it here: https://unix.stackexchange.com/questions/1288/preserve-bash-...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: