From 31982e202d2e939e5c35d6a99f97345b43b8d503 Mon Sep 17 00:00:00 2001 From: George Nachman Date: Tue, 2 Jun 2026 11:16:25 -0700 Subject: [PATCH] emit OSC 133 k=r and k=s for iTerm2 >= 3.7.0 (#2954) iTerm2 3.7.0 added receiver support for the OSC 133 k= attribute (Semantic Prompt prompt kinds: k=i initial, k=s secondary/PS2, k=c continuation, k=r right). When emitted by the shell, iTerm2 records the bracketed cells as non-input on the active primary prompt mark, so consumers (Select Current Command, share-as-URL, AI prompt extraction, the Python GetPromptResponse.excluded_subranges field) can subtract them from the typed-command range. Two gaps for iTerm2 today: 1. The RPROMPT k=r wrap in _p9k_init_prompt is gated to WarpTerminal. iTerm2 sessions never trip the gate, so right-prompt cells leak into "Select Current Command", share URLs, etc. 2. p10k replaces iTerm2's iterm2_decorate_prompt with a stub that only records ITERM2_PRECMD_PS1. iTerm2's original function also wraps PS2 with OSC 133; the stub drops that wrap entirely, so multi-line input (for, while, unclosed quotes, heredocs, backslash continuations) produces no k=s markers and iTerm2 has no way to tell PS2-prefix cells apart from user input. Both fixes are gated on iTerm2 >= 3.7.0 via TERM_PROGRAM_VERSION == (3.<7->*|<4->.*). Older iTerm2 receivers do not parse k= and would treat k=r/k=s markers as spurious primary prompts (extra navigation marks, broken command-range tracking, broken paste-each-line semantics). WarpTerminal behavior is unchanged. The ITERM2_SQUELCH_PS2_MARK opt-out from iTerm2's original iterm2_decorate_prompt is preserved. --- internal/p10k.zsh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 4f3c91f2..064bbd3a 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -8399,14 +8399,16 @@ _p9k_init_prompt() { if (( _POWERLEVEL9K_TERM_SHELL_INTEGRATION )); then _p9k_prompt_prefix_left+=$'%{\e]133;A\a%}' _p9k_prompt_suffix_left+=$'%{\e]133;B\a%}' - if [[ $TERM_PROGRAM == WarpTerminal ]]; then + if [[ $TERM_PROGRAM == WarpTerminal || + ( $TERM_PROGRAM == iTerm.app && $TERM_PROGRAM_VERSION == (3.<7->*|<4->.*) ) ]]; then _p9k_prompt_prefix_right=$'%{\e]133;P;k=r\a%}'$_p9k_prompt_prefix_right _p9k_prompt_suffix_right+=$'%{\e]133;B\a%}' fi if (( $+_z4h_iterm_cmd && _z4h_can_save_restore_screen == 1 )); then _p9k_prompt_prefix_left+=$'%{\ePtmux;\e\e]133;A\a\e\\%}' _p9k_prompt_suffix_left+=$'%{\ePtmux;\e\e]133;B\a\e\\%}' - if [[ $TERM_PROGRAM == WarpTerminal ]]; then + if [[ $TERM_PROGRAM == WarpTerminal || + ( $TERM_PROGRAM == iTerm.app && $TERM_PROGRAM_VERSION == (3.<7->*|<4->.*) ) ]]; then _p9k_prompt_prefix_right=$'%{\ePtmux;\e\e]133;P;k=r\a\e\\%}'$_p9k_prompt_prefix_right _p9k_prompt_suffix_right+=$'%{\ePtmux;\e\e]133;B\a\e\\%}' fi @@ -8965,6 +8967,11 @@ _p9k_init() { function iterm2_decorate_prompt() { typeset -g ITERM2_PRECMD_PS1=$PROMPT typeset -g ITERM2_SHOULD_DECORATE_PROMPT= + if [[ -n $PS2 && $PS2 != *$'\e]133;A;k=s\a'* && -z ${ITERM2_SQUELCH_PS2_MARK-} && + $TERM_PROGRAM_VERSION == (3.<7->*|<4->.*) ]]; then + typeset -g ITERM2_PRECMD_PS2=$PS2 + PS2=$'%{\e]133;A;k=s\a%}'$PS2$'%{\e]133;B\a%}' + fi } fi if (( $+functions[iterm2_precmd] )); then