276

Can I save tabs on exit in my iTerm 2, so I don't have to open same directories again after restart?

Like Chrome does, for example.

I couldn't find such option in settings. Maybe "Arrangements" is what I want, but it seems that it requires me to explicitly save tabs.

6
  • iTerm 2 will open your saved Window Arrangements if you need that and don't like iTerm or Terminal.app on Lion. It works on Snow Leopard as well.
    – bmike
    Commented Aug 18, 2011 at 19:40
  • ouch, I had no idea that iTerm 2 is something different from iTerm. I used iTerm 2. Can it save Window Arrangement on exit? I couldn't find out how
    – valya
    Commented Aug 18, 2011 at 20:03
  • 1
    You have to explicitly save the settings from my brief read of the FAQ - we'll see if anyone here knows for sure now that the question is razor sharp :-) Nice edits.
    – bmike
    Commented Aug 18, 2011 at 20:05
  • 1
    iTerm2 has no support for reopening directories. This ticket can be used to track the implementation: code.google.com/p/iterm2/issues/detail?id=2775 Commented Sep 16, 2014 at 22:00
  • 1
    It's a nightmare, but really useful once you set it up. I documented the full process in a blog post which is too long to post on StackOverflow.
    – Andy Ray
    Commented Jun 6, 2017 at 22:37

13 Answers 13

234

You can Save and Restore Window Arrangement with ⇧ ⌘ S and ⇧ ⌘ R options under the Window screen in iTerm2

You can start the default Arrangement Option to enable are at Preferences -> General -> Startup -> Open default window arrangement .

You can add arrangements with the ⇧ ⌘ S and manage them under Preferences -> Arrangements tab .

iTerm Window Options

7
  • 7
    the one detail that was important for me here: first arrange windows like you want to have them, then hit ⇧⌘s and save as 'Default' (without the quotes, capitalized). the exact spelling is also shown on the Window > Restore Window Arrangement submenu. any other spelling will save the arrangement alright but not restore it on iTerm restart.
    – flow
    Commented Oct 20, 2013 at 13:52
  • 44
    This does not answer the original question of "Can I save tabs on exit in my iTerm 2, so I don't have to open same directories again after restart?"
    – Jeremy
    Commented Jul 15, 2014 at 17:56
  • @JeremyLee Indeed. One will have to couple Window Arrangement with profile arrangement, like mentioned by Rob below, to achieve this purpose.
    – xji
    Commented Feb 1, 2015 at 8:07
  • 1
    This doesn't do what Chrome does, it seems. Chrome re-opens the tabs that were open when you last quit it. This seems to be more like something Internet Explorer did ages ago, where you could tell what it should open every time you start it, regardless of what was open last time. Commented Jul 7, 2016 at 21:06
  • 2
    @flow You can also go to the arrangements tab in preferences, select a given arrangement and click 'set default' in the lower left hand side of the screen Commented Oct 13, 2017 at 15:18
94
  1. In iTerm go to Preferences -> General and under Startup set Use System Window Restoration Setting. enter image description here

  2. Go to macOS System Preferences -> General and make sure that Close windows when quitting an app is unchecked. enter image description here

7
  • 3
    works great on macOS Catalina (10.15) and iTerm 3.3
    – Gian Tech
    Commented Feb 14, 2020 at 10:30
  • 8
    This answers the question correctly with a simple approach. Thank you.
    – Tyler
    Commented Apr 17, 2020 at 21:38
  • 3
    I feel like there's one more thing at play here. I have the same settings on 2 Macs, both running Big Sur. On my M1 Mac, the tabs are restored and each session is in the directory where I left off. On my Intel Mac, the tabs are restored and I see the tab's history, but every session is in my home directory. Not sure what the difference is.
    – chrispix
    Commented Aug 25, 2021 at 19:00
  • 1
    @chrispix I have the same issue on my M1 macbook and previously had it on my Intel macbook (although some panes within a given tab will automatically use the previous directory -- it's just the primary/selected pane that switches to home). Appears to be a bug. Spend way too much time trying to find the solution to no avail. Surprised more people haven't reported it.
    – Luke Davis
    Commented Nov 13, 2022 at 0:15
  • 3
    In macOS Ventura (13.3.1), this checkbox is found at System Preferences > Desktop & Dock Commented Apr 21, 2023 at 19:16
34

Try iTermocil

iTermocil allows you to setup pre-configured layouts of windows and panes in iTerm2, having each open in a specified directory and execute specified commands.

4
  • Can it also preserve tabs and the working directories of the different tabs (as asked in the question)?
    – nohillside
    Commented Nov 3, 2015 at 7:59
  • @patrix yes, it has to be set in yml file, however this does not restore tabs on iTerm restart
    – markphd
    Commented Nov 4, 2015 at 9:36
  • This is exactly what I needed!
    – dhempler
    Commented Jul 22, 2017 at 13:44
  • 2
    This project seems to be unmaintained as of 2021 :( Commented May 2, 2021 at 14:23
11

Here's what I do, running OSX 10.10.4:

Install iTerm nightly (the latest release). Brew is the easiest:

brew tap caskroom/versions && brew cask install iterm2-nightly

You can run this side-by-side with other versions; I use Spotlight and look at the versions to see which one I'm opening. By default it will be set to enable session restore which will restore tabs and directories. However, you may have to adjust your Apple system preferences in System Prefs>General where Close windows when quitting an app. See this Gitlab issue for a screenshot if that confuses you.

3
  • This is a new feature in at least iTerm 2.9.20150626 beta. They elsewhere confusingly refer to it as part of the iTerm2 version 3 beta. Anyway, automatic session restore rocks!
    – Bluu
    Commented Aug 21, 2015 at 18:22
  • As new as this answer is, it seems that there is currently no iterm2-nightly Homebrew cask. Commented Aug 24, 2015 at 11:43
  • 1
    Thanks David - I updated it to refer to github.com/caskroom/homebrew-versions
    – Ben Creasy
    Commented Aug 24, 2015 at 16:44
9

This is possible with iterm2: https://www.iterm2.com/documentation-restoration.html

You can toggle this feature with Prefs>Advanced>Enable session restoration, but you must restart iTerm2 after changing this setting.

I also had to uncheck "close windows when quitting an app" in my General system preferences: https://support.apple.com/en-us/HT204005.

4

If someone like me is having trouble enabling 'Session Restoration' in iTerm 2.9 beta (or iTerm3 beta how they call it) do next:

  1. Open preferences
  2. General tab
  3. Startup
  4. Change to 'Use System window restoration setting'. Restart.

enter image description here

Probably for tech geeks it's obvious that one should select this option for this feature to work but for me it was not and I have never seen this mentioned anywhere.

1
4

In iTerm2 v2.1.4 there's a setting under Profiles: Reuse previous session's directory. It may have been there in the earlier versions, but I haven't looked.

See screenshot:

Screenshot

4

No, as of today I couldn't find any way to set a layout/tabs with paths and recover it (as Sublime Text does for example) in iTerm (2.9).

Personally, I'm astonished on how a basic feature (I think much more relevant than tabs colours, for example) is still missing on such a mature project.

3

Coming in quite late, but you should first create some profiles and then use Command - Option/Alt - Shift H (H is for horizontal split; or swap in V for vertical split). It will show you a list of your profiles and you can then add those to your split. Once you build the setup you like just like Sairam did above.

Here's a vid explaining: iterm2 Profiles and Window Arrangements.

3

It's still not exactly what you want, but since you can check the $ITERM_SESSION_ID environment variable to see which panel you are in, by combining this with a saved window arrangement, you can have each of your panel to start with different initial path, or just anything you would like to run by default.

For example, my saved windows arrangment is two tabs with two panels in the first tab. So I will have $ITERM_SESSION_ID=w0t0p0 for the first panel in the first tab, $ITERM_SESSION_ID=w0t0p1 for the 2nd panel, and $ITERM_SESSION_ID=w0t1p0 for the single panel in the 2nd tab. 'w' stands for window, 't' for tab, and 'p' for panel. I check these variables in my .zshrc, and run Vim in the w0t0p0, iPython in the w0t1p0, respectively.

1

Disabling the System Preferences -> General -> "Close windows when quitting an app" checkbox did the job for me.

0

Use AppleScripts!

Here is a script from an old project of mine:

#!/usr/bin/osascript
tell application "iTerm2"
  tell current window
    create tab with default profile
  end tell

  tell current session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-api"
    write text "source .development.env.sh"
    write text "npm run dev | roarr"
    split vertically with default profile
  end tell

  tell second session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-api"
  end tell

  tell current window
    create tab with default profile
  end tell

  tell current session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-web-app"
    write text "source .development.env.sh"
    write text "npm run dev | roarr"
    split vertically with default profile
  end tell

  tell second session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-web-app"
  end tell

  tell current window
    create tab with default profile
  end tell

  tell current session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-cli"
    write text "source .development.env.sh"
    write text "npm run dev | roarr"
    split vertically with default profile
  end tell

  tell second session of current tab of current window
    write text "cd ~/Documents/dev/roarr/roarr-cli"
    write text "source .development.env.sh"
    write text "ts-node --transpile-only .bin/demo.ts | ts-node --transpile-only src/bin/index.ts --output-format json"
  end tell

  tell current window
    create tab with default profile
  end tell

  tell current session of current tab of current window
     write text "docker run --rm -p 8123:8123 --name some-clickhouse-server --ulimit nofile=262144:262144 yandex/clickhouse-server"
  end tell
end tell

It is mostly self-explanatory, the gist of which is that it is a sequence of commands that we are telling iTerm2 to perform.

These commands are documented in iTerm2.

While this does not answer the original question of how to "save" the current session, it provides a framework of how to quickly setup your workstation, which is likely what the intent is behind the original question.

-1

I found the solution in iTerm2's official website, check it out here https://www.iterm2.com/documentation-restoration.html.

After setting iTerm2 restores last session just like Chrome. However the process(e.g. npm start) can not be restored.

Works perfectly under iTerm2 build 3.0.14 and macOS Sierra 10.12.6 (16G29). Hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .