Last Updated: December 28, 2016
· zan5hin

Automatically reattach tmux session using iTerm2

Using the "Send text at start" option in iTerm2, it is possible to automatically re-attach to an existing tmux session.

Open iTerm2's preferences, then select the 'Profiles' tab, and finally the 'General' pane. In the 'Command' section locate the 'Send text at start:' option and put in this command, substituting your session name for base.

tmux attach -t base || tmux new -s base

Now anytime you start iTerm2 (or create a new window) tmux will first try to attach to a session called base, or, if that session doesn't exist, it will create a new session called base.

As long as you typically use just one session (with multiple windows and panes), this tip will allow you to quit iTerm2 and not lose your tmux session.

Say Thanks

2 Responses
Add your response


Cool, nice share!

over 1 year ago ·

Just in time tip. I was setting up my iTerm2 to work with tmux.
As I use tmuxinator, I replaced the second part:

tmux attach -t default || mux start default

Thanks for the great tip anyway. My OSX setup feels nicer.

over 1 year ago ·