Last active
October 25, 2024 08:54
-
-
Save vyder/96891b93f515cb4ac559e9132e1c9086 to your computer and use it in GitHub Desktop.
iterm.bash - Launch iTerm from command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 $@ |
This script has two issues:
- The last line blows up by recursively creating more iterm windows. This line need to be removed.
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:
- make a file called .iterm, add this content to it, and place under ~/.iterm
- in your ~/.profile, add the following line
source ~/.iterm
- Start a new console, and now do
iterm .
and it works!!
Tested with zsh
and on M1 MBP
Thanks for the update!
I don't use this function anymore in my workflow and haven't kept up with it
👍 hopefully the updated version helps someone else the way yours helped me!
🙌
Is there anything fancy I need to do in advance to get this to work?
Simply iterm.bash .
is not doing anything for me
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
Here is a mod that works under V3+ of iTerm2. It is not backward compatible
https://gist.github.com/aberezin/f1e15db7181cdf062c3e7fb5ef6bc735