fix(clipboard): improve OSC 52 escape sequence for SSH compatibility
Summary
- Fix OSC 52 clipboard not working over SSH
- Fix GNU Screen passthrough using wrong format
- Add nested tmux session support
- Use ST terminator instead of BEL for better compatibility
Problem
Clipboard copy operations fail when running OpenCode over SSH. The existing OSC 52 implementation had several issues:
- Used BEL (
\x07) terminator which some terminals don't handle correctly inside DCS sequences - Applied tmux-style passthrough (
tmux;prefix) to GNU Screen, which uses a different format - No support for nested tmux sessions
Fixes https://github.com/sst/opencode/issues/6111 Fixes https://github.com/anomalyco/opencode/issues/2773
Solution
1. BEL → ST Terminator
- const osc52 = `\x1b]52;c;${base64}\x07`
+ const osc52 = `\x1b]52;c;${base64}\x1b\\`
ST (String Terminator) is more reliably handled inside DCS passthrough sequences.
2. Correct GNU Screen Format
// GNU Screen: DCS without tmux; prefix
const wrapped = osc52.replace(/\x1b/g, "\x1b\x1b")
sequence = `\x1bP${wrapped}\x1b\\`
3. Nested tmux Support
Detect nesting level from TMUX environment variable (comma-separated) and double escapes for each level:
const tmuxLevel = (process.env["TMUX"]?.match(/,/g) || []).length + 1
for (let i = 0; i < tmuxLevel; i++) {
wrapped = wrapped.replace(/\x1b/g, "\x1b\x1b")
wrapped = `\x1bPtmux;${wrapped}\x1b\\`
}
Supported Environments
| Environment | Status |
|---|---|
| iTerm2 | ✅ |
| Kitty | ✅ |
| Alacritty | ✅ |
| Windows Terminal | ✅ |
| tmux | ✅ |
| Nested tmux | ✅ |
| GNU Screen | ✅ |
Test plan
- [x] Tested copy operation over SSH session
- [x] Verified paste works on local machine
- [ ] Test inside tmux
- [ ] Test inside GNU Screen
- [ ] Test nested tmux (tmux inside tmux)
🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.5 [email protected]
The following comment was made by an LLM, it may be inaccurate:
Based on my search, I found one potentially related PR:
PR #7028: fix(clipboard): add OSC 52 fallback for SSH/tmux environments https://github.com/anomalyco/opencode/pull/7028
This PR appears to address similar issues with OSC 52 clipboard functionality in SSH and tmux environments. You should check if this PR was already merged or if it's still open, as it may cover some or all of the same fixes being proposed in PR #9008.
this should be resolved in by #8974
Can we just get an option to disable the clipboard outright so that we can use the normal right click copy and paste?
that was built -> OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT https://github.com/anomalyco/opencode/issues/4751