Dkova Dkova - 1 year ago 152
Bash Question

new tab in 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"

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

Answer Source

iTerm2 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"
        tell current window to set tb to create tab with default profile
        tell current session of current window to write text "pwd"  
      end tell

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 browse iTerm2's available AppleScript commands, open Script, select File > Open Dictionary..., and then

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