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

voi*_*ter 5 macos tabs iterm tmux iterm2

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

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

Par*_*lor 10

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

从iTerm2主菜单:

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


Rya*_*acG 0

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

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

如果您使用的是 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
}
Run Code Online (Sandbox Code Playgroud)

它使用 AppleScript 的 CLI,似乎对我来说工作得很好。