272

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.

valya
  • 4,033
  • 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 Aug 18 '11 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 Aug 18 '11 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 Aug 18 '11 at 20:05
  • 1
    iTerm2 has no support for reopening directories. This ticket can be used to track the implementation: https://code.google.com/p/iterm2/issues/detail?id=2775 – Jörn Zaefferer Sep 16 '14 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 Jun 06 '17 at 22:37
  • My problem fixed by unchecking close windows when quitting an app in Preferences>General – Emadpres Jun 14 '19 at 19:16

13 Answers13

231

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

Sairam
  • 5,736
  • 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 Oct 20 '13 at 13:52
  • 42
    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 Jul 15 '14 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 Feb 01 '15 at 08: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. – Daniel C. Sobral Jul 07 '16 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 – Adam Tolley Oct 13 '17 at 15:18
  • The user experience of saving/restoring the state of your iterm2 windows and tabs is low hanging fruit for a big win in an overall improvement of the app, imho. – NOCARRIER Jun 30 '21 at 17:17
  • @flow using 'Default' doesn't override an old default – SuperUberDuper Dec 22 '22 at 13:20
91
  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

Kuba
  • 1,011
  • 3
    works great on macOS Catalina (10.15) and iTerm 3.3 – Gîanfranco P. Feb 14 '20 at 10:30
  • 8
    This answers the question correctly with a simple approach. Thank you. – Tyler Apr 17 '20 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 Aug 25 '21 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 Nov 13 '22 at 00:15
  • 1
    @LukeDavis I seem to have resolved this. I got an M1 MBP and restored it from the backup of my Intel Mac, which caused some other problems but fixed this, making me realize it’s not necessarily an M1 issue. I think I then copied the preferences plists over to my M1 mini, which fixed it there as well. So I feel like it’s something in the plists that isn’t exposed in the UI. – chrispix Nov 14 '22 at 02:44
  • I also noticed I had to actually exit all my tabs and open new ones before restoration started working. – chrispix Nov 14 '22 at 02:45
  • 3
    In macOS Ventura (13.3.1), this checkbox is found at System Preferences > Desktop & Dock – wileyquixote Apr 21 '23 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.

markphd
  • 441
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.

  • 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 Aug 21 '15 at 18:22
  • As new as this answer is, it seems that there is currently no iterm2-nightly Homebrew cask. – David Resnick Aug 24 '15 at 11:43
  • 1
    Thanks David - I updated it to refer to https://github.com/caskroom/homebrew-versions – Ben Creasy Aug 24 '15 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.

qix
  • 223
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.

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

maksimov
  • 140
  • 4
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.

Edoardoo
  • 141
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.

Metaphox
  • 139
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.

bassplayer7
  • 14,106
Rob
  • 131
1

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

mFlorin
  • 119
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.

Gajus
  • 337
-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.

swen
  • 141
  • 2