Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

My understanding is that with Lion my terminal windows should restore their state. However, this does not happen for me. I wonder if this is because I use tcsh instead of bash?

Is there something I can do to make my iterm2 sessions resume where they left off?

My current attempt to do this has been to use

 alias precmd 'if ( $?TERM_PROGRAM && ${TERM_PROGRAM} == "iTerm.app" ) /opt/local/libexec/gnubin/echo -ne "\033];$cwd\007"'
 set prompt = '%{\e]2;%~\a%}%S[%m:%c3] %n%#%s '

But this sets only the labels of the tabs. The label of the window gets the abbreviated filenames with ~ in place of the full filename. I don't know if this is related to my failure to get terminal resumption. At any rate, when I quit and restart all my terminal windows and tabs are back in my home directory.

Extra bonus points would be a solution that is sensitive to use of slogin.

I am unable to get the correct resumption behavior with the built-in Apple Terminal, either, when I change the above to

if ( $?TERM_PROGRAM && ( ${TERM_PROGRAM} == "iTerm.app" || ${TERM_PROGRAM} == "Apple_Terminal") ) then
  alias precmd ' /opt/local/libexec/gnubin/echo -ne "\033];$cwd\007"'
  set prompt = '%{\e]2;%~\a%}%S[%m:%c3] %n%#%s '
endif

I don't return to the same working directory, and while I can see the command history, I cannot use it (I can't use ^P to move up, e.g., and the history command returns nothing). So I believe somehow this is not working with tcsh, as opposed to bash. My colleagues assure me that they don't lose the working directory upon application exit.

share|improve this question
3  
Lion saves and shows the contents of the scroll back buffers (and only for the built in Terminal app - iTerm2 and others are on their own for supporting sudden termination and application resumption). Things like working directory and shell variables are not maintained. Are you looking to use screens as your shell to save that sort of content while the Mac runs, but the app Terminal is closed? –  bmike Nov 14 '12 at 15:13
    
bash is the default shell on OS X, and only it is configured by default to coordinate with Terminal for restoring the working directory. You can see the code that does this in /etc/bashrc. You may need to find someone to adapt that for tcsh. Elsewhere on Stack Exchange I posted equivalent code for zsh. –  Chris Page Jan 15 '13 at 14:19
    
To save and restore arbitrary state, like environment variables, you'll have to add code to your shell configuration script(s) to manage it. For every terminal window/tab, Terminal sets an environment variable $TERM_SESSION_ID to a unique ID; its exact value is subject to change, but it is currently a UUID. When restoring, it restores this ID. For example, you can use the ID to generate a file pathname and save data in it, then read it back in when the shell starts. You'll probably want to periodically delete old session data, since you don't know if a session will ever be restored again. –  Chris Page Jan 15 '13 at 14:22

1 Answer 1

You could add something like this to the startup script of your default shell (mine is zsh, most people use bash, I guess, so you may have to tweak.)

Here's my bit of screen jiggery-pokery (in my .zshrc)

# Autoload screen if we aren't in it.  (Thanks Fjord!)
if [[ $STY = '' ]] then screen -xRR; fi

I got this from somewhere on the interwebs (I don't recall where, and I don't know who Fjord is, but good on him.

With this you can close your iTerm window, and it will resume where you left off when you reopen it. I highly recommend reading the screen man page also.

share|improve this answer
    
Does this work if you have more than one iTerm window open? How would multiple screen sessions hook back up to multiple iTerm sessions? –  Robert P. Goldman Feb 1 '13 at 3:29

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.