Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Please see this post @ Server Fault, it explains how to do this. – Mutant Feb 21 '12 at 16:30
up vote 4 down vote accepted

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.

share|improve this answer
    
Great, thanks! Marking this as solved. Even though it kind of depends on how OSX loads the terminals I have a feeling this is the closest we can get. – Robert Sköld Feb 27 '12 at 13:23
    
Is this also possible per-tab?? – Joshua Muheim Mar 5 '14 at 7:58
    
Each tab is a separate tty login, so it should work. – Seth Noble Mar 5 '14 at 19:20
    
Terminal has explicit support for this type of behavior: when it creates a new terminal it assigns the terminal a unique ID and communicates it to the terminal session via the TERM_SESSION_ID environment variable. When it restores a terminal for Resume it also restores the same session ID so programs can use this to associate arbitrary state with restored terminals. Use this as part of the file name instead of the tty. – Chris Page Aug 17 '15 at 21:01
    
@ChrisPage the problem with this is that the session is different each time you launch a window/tab. So it would create separate histories and not link them across sessions. – Rene Wooller Oct 21 '15 at 23:22

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.

share|improve this answer

You can set a custom $HISTFILE for each of you Terminal instances.

share|improve this answer
    
Is there a way to do this automatically for each Terminal tab/window? And make sure it's using the same $HISTFILE when the computer starts back up? – Robert Sköld Feb 20 '12 at 13:47
1  
Yes, it's possible to do this, but then how would you access again? You'd manually have to juggle files to get to the previous history files. – EmmEff Feb 20 '12 at 14:08
    
@EmmEff Terminal has explicit support for this type of behavior: when it creates a new terminal it assigns the terminal a unique ID and communicates it to the terminal session via the TERM_SESSION_ID environment variable. When it restores a terminal for Resume it also restores the same session ID so programs can use this to associate arbitrary state with restored terminals. – Chris Page Aug 17 '15 at 20:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.