let me first describe the problem that way annoying me for a long long long time.
i use tmux and do my compiling and coding in one window and then many a times i need to open a new window to do some other stuff. the problem i face is that the bash commands that i write in one window are not present in the new window’s bash history.
for example note a typical annoyance
do some work and then create a new window in tmux (C-b c) in order to do some work in the same directory.
and now most probably you will be placed in your home folder if that is where you started tmux initially. so now in the new window i have to do this long change directory command. the long cd command will not be written to bash history unless the shell exits.
this was a major annoyance to me for a long time. i wanted all the bash sessions to share the history concurrently. i found a hack which lets me do what i want.
put the following in your .bashrc
export PROMPT_COMMAND="$PROMPT_COMMAND; history -a" shopt -s histappend
If the histappend shell option is enabled the lines are appended to the history file, otherwise the history file is overwritten. to find out more about histappend just do a man bash and search for histappend.
the history command we refer to here is the bash built-in history command to know more about it do a
yes that is help history **not** man history
if i have understood it correctly i guess the cleverness of the hack is in automatically doing a “history -a” after each command and what better way than to modify the PROMPT_COMMAND which defines how the prompt should be constructed each time. sweet 🙂