在 iTerm2 中使用特定目录打开多个选项卡

Ali*_*Ali 11 bash iterm2 macos

我想知道这是否可能。

我想设置一些脚本或命令来打开 5 个选项卡,每个打开的选项卡都将指定自己的目录

都在同一个窗口

tab 1: open ~/folderA1
tab 2: open ~/folderA2
tab 3: open ~/folderA3
tab 4: open ~/folderA4
tab 5: open ~/folderA5
Run Code Online (Sandbox Code Playgroud)

这是在 Mac OS X 中的 iTerm2 上。

我知道我可以执行 CMD+T 之类的操作,然后使用cd ~/folderA1等等打开它们中的每一个,但是如果有我可以设置的命令或脚本,在执行后它们会立即完成所有这些操作,我很想知道如果有办法这样做。

slh*_*hck 10

更新:较新的 iTerm 要求您更改语法,因此如下所示:

tell application "iTerm"
    tell current window
        create tab with default profile
    end tell
    tell current tab of current window
        set _new_session to last item of sessions
    end tell
    tell _new_session
        select
        write text "cd \"$dir\""
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

另请参阅此处的答案


对于较旧的 iTerm 版本:

从我的答案here中获取脚本,您可以执行以下操作:

launch () {
for dir in ~/folderA{1..5}; do
/usr/bin/osascript <<-EOF
tell application "iTerm"
    make new terminal
    tell the current terminal
        activate current session
        launch session "Default Session"
        tell the last session
            write text "cd \"$dir\""
        end tell
    end tell
end tell
EOF
done
}
Run Code Online (Sandbox Code Playgroud)

解释发生了什么:

  • 我们创建了一个名为 的 shell 函数launch,因此您可以将它放在您的~/.bash_profile或任何您希望在启动时执行的地方。

  • 我们循环遍历 Bash 大括号扩展的结果~/folderA{1..5},让您~/folderA1通过~/folderA5.

  • 我们通过调用 iTerm2 AppleScript 库osascript来创建一个新选项卡,激活它,启动默认会话,并cd转到指定目录。


小智 8

itermocil可以解决这个问题。

在名为 的文件中添加以下内容~/.itermocil/foo.yml,该命令itermocil foo将在指定文件夹中打开 5 个选项卡。(不过,这是一个非常简单的布局 - itermocil 的功能远不止于此。)

windows:
  - name: '1'
    root: ~/folderA1
    layout: even-horizontal
    panes:
      - focus: true
  - name: '2'
    root: ~/folderA2
    layout: even-horizontal
    panes:
      - focus: true
  - name: '3'
    root: ~/folderA3
    layout: even-horizontal
    panes:
      - focus: true
  - name: '4'
    root: ~/folderA4
    layout: even-horizontal
    panes:
      - focus: true
  - name: '5'
    root: ~/folderA5
    layout: even-horizontal
    panes:
      - focus: true
Run Code Online (Sandbox Code Playgroud)