Skip to content

Instantly share code, notes, and snippets.

@vyder
Last active March 24, 2024 21:59
Show Gist options
  • Save vyder/96891b93f515cb4ac559e9132e1c9086 to your computer and use it in GitHub Desktop.
Save vyder/96891b93f515cb4ac559e9132e1c9086 to your computer and use it in GitHub Desktop.
iterm.bash - Launch iTerm from command line
#!/bin/bash
#
# Open new iTerm window from the command line
#
# Usage:
# iterm Opens the current directory in a new iTerm window
# iterm [PATH] Open PATH in a new iTerm window
# iterm [CMD] Open a new iTerm window and execute CMD
# iterm [PATH] [CMD] ... You can prob'ly guess
#
# Example:
# iterm ~/Code/HelloWorld ./setup.sh
#
# References:
# iTerm AppleScript Examples:
# https://gitlab.com/gnachman/iterm2/wikis/Applescript
#
# Credit:
# Inspired by tab.bash by @bobthecow
# link: https://gist.github.com/bobthecow/757788
#
# OSX only
[ `uname -s` != "Darwin" ] && return
function iterm () {
local cmd=""
local wd="$PWD"
local args="$@"
if [ -d "$1" ]; then
wd="$1"
args="${@:2}"
fi
if [ -n "$args" ]; then
# echo $args
cmd="; $args"
fi
osascript &>/dev/null <<EOF
tell application "iTerm"
activate
set term to (make new terminal)
tell term
launch session "Default Session"
tell the last session
delay 1
write text "cd $wd$cmd"
end
end
end tell
EOF
}
iterm $@
@vyder
Copy link
Author

vyder commented Apr 7, 2021

Unfortunately I don't use this script in my workflow anymore - so I haven't been maintaining it.

Try one of the forks others have created - that might work better for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment