Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Last active March 15, 2023 03:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STAR-ZERO/5928936 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/5928936 to your computer and use it in GitHub Desktop.
動的にiTerm2の背景画像を変える

動的にiTerm2の背景画像を変える

こんな感じのをzshrcに書く。この場合、@キーで画像が変わる。

image_list=("画像1" "画像2") # 絶対パス
image_index=1
bg() {
    if [ -z "$BUFFER" ]; then
        if test $image_index -eq 2; then
            image_index=1
        else
            image_index=$(( $image_index + 1 ))
        fi
        image_path=$image_list[$image_index]
        osascript -e "tell application \"iTerm\"
          set current_terminal to (current terminal)
          tell current_terminal
            set current_session to (current session)
            tell current_session
              set background image path to \"$image_path\"
            end tell
          end tell
        end tell"
    else
        zle self-insert '@'
    fi
}
zle -N bg
bindkey '\@' bg

コマンドが空でエンターを押すと背景画像が変わる

image_list=("画像1" "画像2")
image_index=1
bg() {
    if [ -z "$BUFFER" ]; then
        if test $image_index -eq 2; then
            image_index=1
        else
            image_index=$(( $image_index + 1 ))
        fi
        image_path=$image_list[$image_index]
        osascript -e "tell application \"iTerm\"
          set current_terminal to (current terminal)
          tell current_terminal
            set current_session to (current session)
            tell current_session
              set background image path to \"$image_path\"
            end tell
          end tell
        end tell"
        zle reset-prompt
        zle accept-line
    else
        zle accept-line
    fi
}
zle -N bg
bindkey '^m' bg

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