iTerm2和tmux:在当前选项卡的工作目录中打开新选项卡

时间:2015-04-18 14:51:30

标签: macos tabs tmux iterm iterm2

当我使用iTerm2和tmux在远程shell上打开一个新选项卡(通过⌘T)时,我几乎总是希望新选项卡与当前选项卡具有相同的工作目录。我能做的最好的事情是让iTerm2在我运行tmux -CCtmux -CC attach的同一目录中打开新标签页。 (可以通过导航到首选项→配置文件→常规→工作目录来配置此行为。)

此目录不会必然是当前选项卡的工作目录。有没有办法得到我正在寻找的行为?我在网上搜索了一段时间但找不到任何有用的信息。

3 个答案:

答案 0 :(得分:7)

使用Iterm2时,如果您希望通过⌘T在与当前选项卡相同的目录中打开新选项卡,则可以在首选项下的配置文件中设置一个选项。

来自iTerm2主菜单

Iterm2 -> Preferences -> profiles -> working directory -> Reuse Previous Session Directory

答案 1 :(得分:0)

如果你正在使用ZSH,你可以尝试这样的事情;

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

如果您正在使用bash,我不确定相应的内容是什么。此外,如果您使用prezto或Oh-My-ZSH,则选项卡功能已经内置。

<强>更新

看过prezto是如何做到的,这应该是完整的解决方案

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
}

它使用Apple for AppleScript,似乎对我来说很好。

答案 2 :(得分:0)

使用tmux,一种解决方案是在您的alias itab='open . -a iterm'中设置.bash_alias