Skip to content

Instantly share code, notes, and snippets.

@vyder
Last active March 24, 2024 21:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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 $@
@aberezin
Copy link

Here is a mod that works under V3+ of iTerm2. It is not backward compatible

https://gist.github.com/aberezin/f1e15db7181cdf062c3e7fb5ef6bc735

@geyang
Copy link

geyang commented Jul 14, 2020

This script has two issues:

  1. The last line blows up by recursively creating more iterm windows. This line need to be removed.
  2. iterm . does not resolve the path correctly because . is called in the new window.

My version of this script fixes both issues: gist/geyang/iterm.bash

Update from 2022-07-07

This works for me! Steps to get it to work:

  1. make a file called .iterm, add this content to it, and place under ~/.iterm
  2. in your ~/.profile, add the following line
    source ~/.iterm
  3. Start a new console, and now do
    iterm .

and it works!!

Tested with zsh and on M1 MBP

@vyder
Copy link
Author

vyder commented Jul 21, 2020

Thanks for the update!

I don't use this function anymore in my workflow and haven't kept up with it

@geyang
Copy link

geyang commented Jul 22, 2020

👍 hopefully the updated version helps someone else the way yours helped me!

@vyder
Copy link
Author

vyder commented Jul 29, 2020

🙌

@theRealSuperMario
Copy link

Is there anything fancy I need to do in advance to get this to work?

Simply iterm.bash . is not doing anything for me

@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