How can I get the Terminal to keep a separate history per tab/window?

You could do something like this in your .bashrc:

MYTTY=`tty`
HISTFILE=$HOME/.bash_history_`basename $MYTTY`

This will create separate history files in your home directory such as .bash_history_ttys002. When you create a new terminal window, it will use the history file corresponding to the tty name, which will usually be determined simply by the order in which you create the terminal windows.


I implemented this by adding

export HISTFILE="$HOME/.bash_history_$TERM_SESSION_ID"
if [ ! -e $HISTFILE ]; then cp "$HOME/.bash_history" "$HISTFILE"; fi

to my ~/.bash_profile file. Now when I close terminal and open it again, each tab has its separate command history restored. Also when I create new tab, default history is initialized from .bash_history

Also to make Terminal app to preserve opened tabs between quit and restart you need to make sure that option "close windows when quitting app" (on Yosemite) is disabled in System Preferences -> General.