iTerm2中的新标签

时间:2016-08-01 06:35:03

标签: bash iterm2 iterm

我正在使用Iterm2版本Build 3.0.4 我想创建别名以从命令行打开新选项卡(在bash中) 我试过这段代码:

    function tab () {
    osascript &>/dev/null <<EOF
activate application "iTerm"
    tell application "System Events" to keystroke "t" using command down
    tell application "iTerm" to tell session -1 of current terminal to write text "pwd"
EOF
}

但它不起作用。任何人都可以使用此版本(或更新版本)解决问题吗?

1 个答案:

答案 0 :(得分:7)

iTerm2 v3具有大大改进的AppleScript支持,因此您现在可以直接创建标签,而无需发送按键:

tab() {
    osascript &>/dev/null <<EOF
      tell application "iTerm"
        activate
        tell current window to set tb to create tab with default profile
        tell current session of current window to write text "pwd"  
      end tell
EOF
}

要水平拆分新标签(按⇧⌘D所示),请添加:

tell current session of current window to split horizontally with same profile

pwd写入拆分创建的新会话(新标签的下半部分):

tab() {
    osascript &>/dev/null <<EOF
      tell application "iTerm"
        activate
        tell current window to set tb to create tab with default profile
        tell current session of current window to set newSplit to split horizontally with same profile
        tell newSplit
          select
          write text "pwd"
        end tell    
      end tell
EOF
}

要浏览iTerm2的可用AppleScript命令,请打开Script Editor.app,选择File > Open Dictionary...,然后选择iTerm.app

还要考虑我的ttab CLI,其中包含选项卡/窗口创建以及Terminal.appiTerm2.app的高级功能(但它不支持拆分选项卡)。