Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new tab in iTerm2

Tags:

bash

iterm

iterm2

Im using Iterm2 version Build 3.0.4 I want to create alias to open new tab from the command line (in bash) I tried this code:

    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
}

but it isn't working. Can anyone solve the problem with this version (or newer version)?

like image 533
Dkova Avatar asked Aug 01 '16 06:08

Dkova


1 Answers

iTerm2 v3 features much-improved AppleScript support, so you can now create tabs directly, without having to send keystrokes:

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
}

To split the new tab horizontally (as you would get by pressing ⇧⌘D), add:

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

To write pwd to the new session created by the split (the lower half of the new tab):

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
}

To browse iTerm2's available AppleScript commands, open Script Editor.app, select File > Open Dictionary..., and then iTerm.app.

Also consider my ttab CLI, which packages tab / window creation along with advanced features for both Terminal.app and iTerm2.app (but it doesn't support splitting a tab).

like image 169
mklement0 Avatar answered Sep 28 '22 07:09

mklement0