From 094d3f97ee781e54d78603c173d655daab92a0c5 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Mon, 7 Sep 2026 17:54:14 -0700 Subject: [PATCH] Fixed iterm2.sh --- .rc.d/iterm2.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/.rc.d/iterm2.sh b/.rc.d/iterm2.sh index 62c2e73..1e3d4df 100755 --- a/.rc.d/iterm2.sh +++ b/.rc.d/iterm2.sh @@ -1,7 +1,60 @@ -function anon() { - local script_path="$HOME/.iterm2_shell_integration.zsh" - if [ "$TERM_PROGRAM" = "iTerm.app" -a -e "$script_path" ]; then - source "$script_path" +# Setup iTerm2 shell integration on macOS +_setup_iterm2() { + # Only run on macOS + [ "$(uname -s 2>/dev/null)" = "Darwin" ] || return 0 + + # Only run if iTerm.app is available in /Applications or $HOME/Applications + _iterm_app="" + for _app_cand in \ + "/Applications/iTerm.app" \ + "$HOME/Applications/iTerm.app" \ + "/Applications/ITerm.app" \ + "$HOME/Applications/ITerm.app" \ + "/Applications/iTerm2.app" \ + "$HOME/Applications/iTerm2.app" + do + if [ -d "$_app_cand" ]; then + _iterm_app="$_app_cand" + break + fi + done + + [ -n "$_iterm_app" ] || return 0 + + # Add iTerm2 utilities (imgcat, it2dl, etc.) to PATH if available + if [ -d "$_iterm_app/Contents/Resources/utilities" ]; then + case ":$PATH:" in + *":$_iterm_app/Contents/Resources/utilities:"*) ;; + *) PATH="$_iterm_app/Contents/Resources/utilities:$PATH"; export PATH ;; + esac + fi + + # Only activate shell integration when running inside iTerm + if [ "$TERM_PROGRAM" = "iTerm.app" ] || [ -n "$ITERM_SESSION_ID" ] || [ "$LC_TERMINAL" = "iTerm2" ]; then + _shell_ext="" + if [ -n "$ZSH_VERSION" ]; then + _shell_ext="zsh" + elif [ -n "$BASH_VERSION" ]; then + _shell_ext="bash" + elif [ -n "$FISH_VERSION" ]; then + _shell_ext="fish" + elif [ -n "$tcsh" ]; then + _shell_ext="tcsh" + fi + + if [ -n "$_shell_ext" ]; then + _script_path="" + if [ -r "$HOME/.iterm2_shell_integration.$_shell_ext" ]; then + _script_path="$HOME/.iterm2_shell_integration.$_shell_ext" + elif [ -r "$_iterm_app/Contents/Resources/iterm2_shell_integration.$_shell_ext" ]; then + _script_path="$_iterm_app/Contents/Resources/iterm2_shell_integration.$_shell_ext" + fi + + if [ -n "$_script_path" ]; then + \. "$_script_path" + fi + fi fi } -unset -f anon +_setup_iterm2 +unset -f _setup_iterm2