Live data from Hacker News

Keeping bash history in sync between multiple terminals

briancarper.net

11–20 of 35 posts

Re: Keeping bash history in sync between multiple terminals

#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

Re: Keeping bash history in sync between multiple terminals

#14
I store a separate bash history per directory, in a tree under ~/.bash_history.d. Each time I change directories, my shell flushes its history to that directory's histfile, and loads in the histfile for the new directory.

It may sound weird, but look at this way: Usually I cd to a "place" to work on some particular project. When a cd to some project I haven't touched in a while, it's very useful to have, in 'history', a context – the usual commands I use to start up that server, the last few files I fiddled with, etc.

Anyone else do something like this?

Re: Keeping bash history in sync between multiple terminals

#15
post #9

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

I keep my ~/.bashrc in my Dropbox and symlink to it. You could do the same with ~/.bash_history.

Yeah, I do this as well. It is often very useful.

Re: Keeping bash history in sync between multiple terminals

#16

It doesn't seem to work for me unless I switch the two commands: PROMPT_COMMAND='history -a;history -n'

I was having some weird problems two, with some commands not even being recorded.

As people on the blog commented, I've followed and only include `history -a`. Running `history -n` would screw up the workflow of pressing the up cursor to scroll through the current terminal's history.

Re: Keeping bash history in sync between multiple terminals

#17

I store a separate bash history per directory, in a tree under ~/.bash_history.d. Each time I change directories, my shell flushes its history to that directory's histfile, and loads in the histfile for the new directory. It may sound weird, but look at this way: Usually I cd to a "place" to work on some particular project. When a cd to some project I haven't touched in a while, it's very useful to have, in 'history'…

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

Re: Keeping bash history in sync between multiple terminals

#19

I store a separate bash history per directory, in a tree under ~/.bash_history.d. Each time I change directories, my shell flushes its history to that directory's histfile, and loads in the histfile for the new directory. It may sound weird, but look at this way: Usually I cd to a "place" to work on some particular project. When a cd to some project I haven't touched in a while, it's very useful to have, in 'history'…

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 directory are probably clobbering each other -- looks like this 'shopt -s histappend' would help with that.

Post reply on HN