
Keeping bash history in sync between multiple terminals - patpng
http://briancarper.net/blog/248/
======
aaronharnly
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?

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

~~~
aaronharnly
Sure -- here is the actual version from my dotfiles:

[https://github.com/aaronharnly/dotfiles-
public/blob/master/....](https://github.com/aaronharnly/dotfiles-
public/blob/master/.bash-functions.sh#L263)

(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.

~~~
possibilistic
I am totally going to try this out! Thanks!

------
dave1010uk
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.

~~~
6ren
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

~~~
kahawe
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...

------
shaggy
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.

------
arbrandes
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}"

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

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

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

------
eneveu
I really like the "ctrl + o" trick mentioned in the third comment: "Normally,
if you enter a series of commands, you can go back to the first one, and hit
Ctrl-O repeatedly to run the current command and retrieve the next."

Found a description here: <http://www.halotis.com/2010/07/06/bash-keyboard-
shortcuts/>

------
joelthelion
Don't overwrite the PROMPT_COMMAND like this, you risk breaking other tools
such as autojump. Instead, append to it.

~~~
pavel_lishin
How?

~~~
joelthelion
This is how autojump does it:

    
    
        export PROMPT_COMMAND="$AUTOJUMP ; ${PROMPT_COMMAND:-:}"

~~~
pavel_lishin
My bash-fu is weak; can you break that down for me?

~~~
zakj
${VARIABLE:-value} expands to the contents of $VARIABLE, if it exists, or to
"value" otherwise. In this case value is ":", a bash builtin that does
nothing. Effectively, the above sets PROMPT_COMMAND to run the autojump
command, then to run the pre-existing PROMPT_COMMAND, if any, or a no-op.

------
ordinary
It doesn't seem to work for me unless I switch the two commands:

    
    
      PROMPT_COMMAND='history -a;history -n'

~~~
mise
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.

------
vitno
use ZSH! the autocomplete is much nicer as well. (check out oh-my-zsh for
awesome configs)

------
robwgibbons
I use a tmux session on my server, so all I need is to log in from any
computer and bring the session back up

~~~
pavel_lishin
But... you still use different windows and panes, right?

------
willlll
use zsh, problem solved

~~~
akkartik
How?

(I use zsh. I also downvoted you.)

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

~~~
JadeNB
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.").

------
sxsde
File not found.

