9

When I open a new tab (via ⌘T) on a remote shell using iTerm2 and tmux, I almost always want the new tab to have the same working directory as the current tab. The best I can do is make iTerm2 open up the new tab in the same directory in which I ran tmux -CC or tmux -CC attach. (This behavior can be configured by navigating to Preferences → Profiles → General → Working Directory.)

This directory will not necessarily be the working directory of the current tab. Is there any way to get the behavior I'm looking for? I searched online for a while but could not find any helpful information.

void-pointer
  • 14,247
  • 11
  • 43
  • 61

3 Answers3

17

When using Iterm2, if you want a new tab to open in the same directory as the current tab via ⌘T, there is an option available in your profile under preferences.

From the iTerm2 main menu:

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

Parker Tailor
  • 1,290
  • 13
  • 12
  • 1
    This will change the behavior for new windows as well. The `Advanced Configuration` (at the same location as given by Parker Tailor) allows a separate setting each for "new windows", "new tabs" and "new split panes". – tbrlpld Oct 08 '19 at 23:25
  • 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 in your `PROMPT_COMMAND` or in an overridden `cd` command) – Cyberwiz Aug 21 '23 at 15:28
0

If you're using ZSH you could try something like this;

function tab() {
  local command="cd \\\"$PWD\\\"; clear; "
  (( $# > 0 )) && command="${command}; $*"
}

If you're using bash I'm not sure what the equivalent would be. Also if you're using prezto or Oh-My-ZSH the tab function is already built in.

UPDATE

Having had a look at how prezto does it, this should be the full solution

local command="cd \\\"$PWD\\\""
(( $# > 0 )) && command="${command}; $*"

the_app=$(
  osascript 2>/dev/null <<EOF
    tell application "System Events"
      name of first item of (every process whose frontmost is true)
    end tell
EOF
)

[[ "$the_app" == 'iTerm' ]] && {
  osascript 2>/dev/null <<EOF
    tell application "iTerm"
      set current_terminal to current terminal
      tell current_terminal
        launch session "Default Session"
        set current_session to current session
        tell current_session
          write text "${command}"
        end tell
      end tell
    end tell
EOF
}

It uses the CLI for AppleScript and seems to work fine for me.

RyanMacG
  • 339
  • 3
  • 20
  • I use ZSH and added this to my `.profile`, but it didn't work. Are you sure this answer was supposed to refer to the iTerm2 native tabs, and not the emulated tabs? – void-pointer May 04 '15 at 02:21
  • It should, it's a component of the tab function from Oh-My-ZSH. I'll have a better look today and see if I can work out what's wrong with it – RyanMacG May 05 '15 at 08:13
  • @void-pointer that should be an updated version that works with iTerm now – RyanMacG May 05 '15 at 08:20
0

With tmux, one solution is to set alias itab='open . -a iterm' in your .bash_alias.

Kevin Powell
  • 591
  • 1
  • 5
  • 20