57

Often in workflow you are working within a directory and then need to create a second terminal or session to the same directory.

TAB 1
#~/home
cd really
#~/home/really
cd long
#~/home/really/long
cd filepath
#~/home/really/long/filepath
node filepath
#session now occupied 

#new TAB
#CTR + T
#~/home
#I want to be in the same filepath 

is there a setting somewhere to do this?

StanleyZheng
  • 4,008
  • 3
  • 21
  • 24

3 Answers3

76

One of the options for a profile is which directory to use when creating a session with that profile. The default is to use your home directory, but you can change that to use the previous directory instead. You can change this setting in the default profile (allowing you to continue using -T), or you can create a new profile which uses this setting, and assign a different keyboard shortcut to create a session with that profile.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 71
    Awesome thanks! The Short Version, For Iterm2 Iterm-> Preferences-> profiles -> working directory -> Reuse Previous Session Directory – StanleyZheng Apr 01 '14 at 02:16
  • 7
    for more control: Iterm2 Iterm-> Preferences-> profiles -> Advanced Config [edit button] -> now you can choose a different action for panes or tabs or windows. – Sgnl Oct 16 '15 at 20:10
  • This feature is broken in iTerm2-3.3.7(The most latest one). Is there any other solution? – DianeZhou Jan 19 '20 at 11:21
  • The "Reuse previous session's directory" option is still available in 3.3.8beta. – chepner Jan 19 '20 at 14:37
  • Fantastic! I cannot believe I didn't bother looking this up before, so useful. I'm using 3.3.9 and you can specify advanced behaviour for when you open a new tab, window or split pane. – Darragh Enright Mar 20 '20 at 19:27
  • Does it also work if I connected over `ssh`? @chepner – alper Apr 13 '20 at 16:29
  • If for some reason iterm2 doesnt correctly detect your working directory, you can explicitly tell it, by using `echo -ne "\033]50;CurrentDir=$PWD\a"` (either as part of your `PROMPT_COMMAND` or in an overridden `cd` command) – Cyberwiz Aug 21 '23 at 15:29
36

Preferences->Profiles->Working Directory->Reuse previous session's directory

Jialin
  • 2,415
  • 2
  • 14
  • 10
-7

To open new tab with same directory we can do this.

shift + cmommand + D 
Amrit Dhungana
  • 4,371
  • 5
  • 31
  • 36