Live data from Hacker News

Keeping bash history in sync between multiple terminals

briancarper.net

21–30 of 35 posts

Re: Keeping bash history in sync between multiple terminals

#21
I've found that one thing that was very helpful for me was to keep history per machine. For me having the history make sense in the context of the system function is very valuable and I know that I won't get other commands that I don't need or want to see in my history.

What I do it HISTFILE=$HOME/.history/`hostname`.history so then I have a nice record of what I've done on all the machines I log into. This is particularly nice when you have a central home directory.

Re: Keeping bash history in sync between multiple terminals

#22
post #4

Earlier quoted context omitted.

How? (I use zsh. I also downvoted you.)

He's joking that Zsh isn't Bash and therefor doesn't keep Bash's history in sync.

I think akkartik is responding to willlll (who posted "use zsh, problem solved"), not to silverstorm (who posted "zsh does a terrible job at keeping bash history in sync.").

Re: Keeping bash history in sync between multiple terminals

#23
Great post! I use screen a lot, and always miss having the other terminal histories readily available.

However, reversing PROMPT_COMMAND so that "history -a" comes first makes more sense: this way the last command executed on the current terminal has a better chance of being on top of the list for the next up-arrow (which more closely resembles usual behavior).

Also, as noted above, it's better to append to PROMPT_COMMAND rather than just overwriting it. Thus, my .bashrc now reads:

  export PROMPT_COMMAND="history -a; history -n;${PROMPT_COMMAND}"

Re: Keeping bash history in sync between multiple terminals

#24

A real trick would be keeping the bash history in sync between multiple machines.

From the title, I thought he would do just that at first! But I guess since the HISTFILE is being constantly read like that anyway, you could just use rsync or similar means to keep it in sync across multiple machines.

Re: Keeping bash history in sync between multiple terminals

#25
post #13

I always thought I wanted an in-sync bash history but after a few days of trying I realised it actually slowed me down. I have multiple terminals open (with Terminator, most of the time) and different windows / tabs are for different projects or tasks. If I'm building / configuring a project in 1 window, suddenly having a few apt-gets or log tails interspersed is very unproductive.

Yes, I found that too. It's convenient that up-arrow is context-specific. However, as a commenter on the blog said, the first suggestion avoids the "last logout wins" problem: shopt -s histappend

histappend is a must for me for any decent bash history usage; I like to set HISTCONTROL=ignoredups along with it to keep things clean and I increased HISTSIZE and HISTFILESIZE from the defaults.

A good bash history can often save your behind when trying to reconstruct what you did or when you have to run that one magic command you last ran 5 months ago...

Re: Keeping bash history in sync between multiple terminals

#26

I always thought I wanted an in-sync bash history but after a few days of trying I realised it actually slowed me down. I have multiple terminals open (with Terminator, most of the time) and different windows / tabs are for different projects or tasks. If I'm building / configuring a project in 1 window, suddenly having a few apt-gets or log tails interspersed is very unproductive.

I am going to chime in with a similar sentiment. I also had bash configured like this for awhile when I realized it is not what I wanted.

If you typically use different terminal windows for different tasks (e.g. terminal #1 for compiling, terminal #2 for manpages, terminal #3 for vim, etc.), this will just clutter up your history with items unrelated to what your are doing.

Re: Keeping bash history in sync between multiple terminals

#27

Earlier quoted context omitted.

That is brilliant! Care to point me toward a working implementation? :)

Sure -- here is the actual version from my dotfiles: https://github.com/aaronharnly/dotfiles-public/blob/master/.... (You can skip the stuff wrapping virtualenv, obviously.) Basically just define a function that does the write, set HISTFILE, read, and then elsewhere alias cd=mycd. If anyone gives it a try, I'd love to hear how it works! I think it needs some tweaks -- for example, multiple terminals in the same direc…

I am totally going to try this out! Thanks!
Post reply on HN