【问题标题】:iTerm2 and tmux: open new tab in working directory of current tabiTerm2 和 tmux:在当前选项卡的工作目录中打开新选项卡
【发布时间】:2015-04-18 14:51:30
【问题描述】:

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

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

【问题讨论】:

    标签: macos tabs tmux iterm iterm2


    【解决方案1】:

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

    从 iTerm2 主菜单:

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

    【讨论】:

    • 这也会改变新窗口的行为。 Advanced Configuration(与 Parker Tailor 给出的位置相同)允许为“新窗口”、“新标签”和“新拆分窗格”分别设置。
    【解决方案2】:

    如果您使用的是 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
    }
    

    它为 AppleScript 使用 CLI,似乎对我来说工作正常。

    【讨论】:

    • 我使用 ZSH 并将其添加到我的 .profile,但它不起作用。您确定这个答案应该是指 iTerm2 原生选项卡,而不是模拟选项卡吗?
    • 应该,它是 Oh-My-ZSH 的 tab 功能的一个组件。我今天会好好看看,看看我能不能找出问题所在
    • @void-pointer 应该是现在可以与 iTerm 一起使用的更新版本
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2018-10-24
      相关资源
      最近更新 更多