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

terminal

When I restart my Mac it quits and saves the buffers for each tab properly, since it's there again when the computer starts back up. But the command history is not buffered properly per window/tab. They're all the same buffer.

I'm assuming it's because bash uses the same ~/.bash_history for all terminals but is there a way to avoid improve this? To make bash, or maybe Terminal, store them separately?

Best Answer

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.