Skip to content

fix(tmux): replace pbcopy with cross-platform clipboard command - #1220

Merged
shunkakinoki merged 1 commit into
mainfrom
fix/tmux-cross-platform-clipboard
Mar 20, 2026
Merged

fix(tmux): replace pbcopy with cross-platform clipboard command#1220
shunkakinoki merged 1 commit into
mainfrom
fix/tmux-cross-platform-clipboard

Conversation

@shunkakinoki

@shunkakinoki shunkakinoki commented Mar 19, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replaces hardcoded pbcopy (macOS-only) with a portable clipboard command defined once as @clipboard_cmd
  • Detects environment at runtime: pbcopy on macOS, wl-copy on Wayland, xclip on X11
  • Applied to vi copy-mode yank, mouse drag copy, and pane history yank bindings

Test plan

  • On macOS: yank in copy-mode copies to clipboard via pbcopy
  • On Linux Wayland: yank copies via wl-copy
  • On Linux X11: yank copies via xclip

🤖 Generated with Claude Code


Summary by cubic

Replace macOS-only pbcopy in tmux with a cross‑platform clipboard command defined as @clipboard_cmd. It auto-detects the environment (pbcopy on macOS, wl-copy on Wayland, xclip -selection clipboard on X11) and is used for vi copy-mode yank, mouse drag copy, and pane history yanks.

Written for commit 15029e9. Summary will update on new commits.

Replaces hardcoded pbcopy with a portable clipboard handler that detects
the environment: pbcopy on macOS, wl-copy on Wayland, xclip on X11.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 19, 2026 23:17
@mesa-dot-dev

mesa-dot-dev Bot commented Mar 19, 2026

Copy link
Copy Markdown

You do not have enough credits to review this pull request. Please purchase more credits to continue.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the tmux configuration to provide a truly cross-platform clipboard experience. By abstracting the clipboard command into a dynamically determined variable, users can now seamlessly copy content across different operating systems and display servers without manual configuration changes, improving usability and consistency.

Highlights

  • Cross-platform clipboard command: Replaced the macOS-specific pbcopy with a portable clipboard command, @clipboard_cmd, for broader compatibility.
  • Environment detection: Implemented runtime detection to use pbcopy on macOS, wl-copy on Wayland, and xclip on X11.
  • Clipboard binding updates: Applied the new @clipboard_cmd to vi copy-mode yank, mouse drag copy, and pane history yank bindings.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai

coderabbitai Bot commented Mar 19, 2026

Copy link
Copy Markdown
📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Tmux clipboard functionality now supports macOS, Linux Wayland, and Linux X11 with automatic detection and fallback support.

Walkthrough

The PR replaces hardcoded pbcopy clipboard utility in tmux configuration with a configurable #{@clipboard_cmd} variable that intelligently selects the appropriate clipboard tool based on system environment, with fallback support for macOS, Wayland, and X11 systems.

Changes

Cohort / File(s) Summary
Tmux Clipboard Configuration
home-manager/programs/tmux/tmux.conf
Replaced hardcoded pbcopy with dynamic #{@clipboard_cmd} across copy-mode bindings and pane yank operations. Added configurable @clipboard_cmd variable with environment-aware fallback chain: pbcopywl-copy (when $WAYLAND_DISPLAY set) → xclip.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 Twitching whiskers with glee!
Copy commands now dance wild and free,
From macOS to Wayland they play,
With fallbacks that save the day,
No more pbcopy chains us down—
Clipboard harmony wears the crown! 📋✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: replacing the platform-specific pbcopy with a cross-platform clipboard command solution.
Description check ✅ Passed The description is directly related to the changeset, providing clear details about the cross-platform clipboard implementation, environment detection logic, and test plan.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/tmux-cross-platform-clipboard
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mesa-dot-dev

mesa-dot-dev Bot commented Mar 19, 2026

Copy link
Copy Markdown

Mesa Description

TL;DR

Replaces hardcoded pbcopy (macOS-only) with a portable clipboard command defined once as @clipboard_cmd

What changed?

  • Replaces hardcoded pbcopy (macOS-only) with a portable clipboard command defined once as @clipboard_cmd
  • Detects environment at runtime: pbcopy on macOS, wl-copy on Wayland, xclip on X11
  • Applied to vi copy-mode yank, mouse drag copy, and pane history yank bindings

Description generated by Mesa. Update settings

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a cross-platform clipboard command for tmux by replacing hardcoded pbcopy calls with a dynamic command stored in a tmux variable. This is a good improvement for portability. However, the new implementation misses a check for reattach-to-user-namespace, which was previously used and is often required for clipboard integration on macOS. I've suggested a modification to the clipboard command to restore this check and ensure broader compatibility.


# Clipboard
set -g set-clipboard on
set -g @clipboard_cmd "command -v pbcopy >/dev/null 2>&1 && pbcopy || { [ -n \"$WAYLAND_DISPLAY\" ] && wl-copy || xclip -selection clipboard; }"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The removal of reattach-to-user-namespace could cause clipboard issues on some macOS systems where it's required for tmux to access the system pasteboard. The original configuration used it for mouse-drag copying.

To ensure compatibility, I recommend updating the @clipboard_cmd to check for reattach-to-user-namespace and use it with pbcopy if it's available. This makes your cross-platform solution more robust.

set -g @clipboard_cmd "command -v reattach-to-user-namespace >/dev/null 2>&1 && reattach-to-user-namespace pbcopy || command -v pbcopy >/dev/null 2>&1 && pbcopy || { [ -n \"$WAYLAND_DISPLAY\" ] && wl-copy || xclip -selection clipboard; }"

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the tmux Home Manager configuration to use a single, runtime-detected clipboard command (macOS/Wayland/X11) instead of hardcoding pbcopy, applying it across multiple copy/yank bindings.

Changes:

  • Adds a global tmux user option @clipboard_cmd that selects pbcopy, wl-copy, or xclip at runtime.
  • Updates copy-mode and mouse selection yank bindings to pipe to @clipboard_cmd.
  • Updates pane-history yank bindings to use the same clipboard command.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +94 to +95
bind y run-shell 'tmux capture-pane -pS - | #{@clipboard_cmd} && tmux display-message "Full pane history copied to clipboard"'
bind v run-shell 'tmux capture-pane -p | #{@clipboard_cmd} && tmux display-message "Visible pane copied to clipboard"'

Copilot AI Mar 19, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The run-shell bindings are referencing @clipboard_cmd as {@clipboard_cmd} (missing the leading #), so tmux will not expand the option and the shell will try to execute a literal {...} token, breaking these bindings. Use the tmux format form #{@clipboard_cmd} here (matching the copy-mode bindings).

Copilot uses AI. Check for mistakes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@home-manager/programs/tmux/tmux.conf`:
- Line 40: The tmux option set -g `@clipboard_cmd` currently uses double quotes so
$WAYLAND_DISPLAY is expanded at tmux parse time; change the value assigned by
set -g `@clipboard_cmd` to use single quotes so the shell evaluates
$WAYLAND_DISPLAY at runtime, and update the shell expression referenced by
`@clipboard_cmd` to check for the existence of wl-copy and xclip (e.g., using
command -v wl-copy >/dev/null 2>&1 && wl-copy) before falling back, ensuring
each fallback has a command-existence test and pbcopy remains checked first.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8ae9e2ac-7924-499f-bf1c-f337502e77a2

📥 Commits

Reviewing files that changed from the base of the PR and between c33e8c1 and 15029e9.

📒 Files selected for processing (1)
  • home-manager/programs/tmux/tmux.conf


# Clipboard
set -g set-clipboard on
set -g @clipboard_cmd "command -v pbcopy >/dev/null 2>&1 && pbcopy || { [ -n \"$WAYLAND_DISPLAY\" ] && wl-copy || xclip -selection clipboard; }"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -nP '^\s*set\s+-g\s+@clipboard_cmd\s+".*\$WAYLAND_DISPLAY.*"$' home-manager/programs/tmux/tmux.conf
# Expected (current state): one match at Line 40
# Expected (after fix): no matches

Repository: shunkakinoki/dotfiles

Length of output: 212


🌐 Web query:

In tmux config, are $ENV vars expanded when parsing set -g @option "..."? What is the recommended way to preserve $WAYLAND_DISPLAY for runtime shell evaluation in clipboard commands?

💡 Result:

No, $ENV vars like $WAYLAND_DISPLAY are not expanded when parsing set -g @option "..." in tmux config. Tmux config parsing replaces $ENV with values from the global environment at parse time, storing the literal expanded string as the user option value. User options (@options) are arbitrary strings without further shell-like expansion. This follows the general command parsing rule where $vars expand during parsing, before execution. The recommended way to preserve $WAYLAND_DISPLAY for runtime shell evaluation in clipboard commands is to use set -s copy-command "wl-copy" (requires tmux 3.2+ and wl-clipboard package). This sets a server option where the command string has $WAYLAND_DISPLAY expanded at config parse time from the global env (inherited when tmux server starts). At runtime, when copy-pipe or similar invokes the copy-command during copy operations, it runs the shell command in a new shell inheriting the current session's environment, which includes $WAYLAND_DISPLAY if preserved via set-environment -g WAYLAND_DISPLAY or update-environment WAYLAND_DISPLAY in .tmux.conf. To ensure $WAYLAND_DISPLAY is available in sessions/shells: set-environment -g WAYLAND_DISPLAY $WAYLAND_DISPLAY or set update-environment "WAYLAND_DISPLAY" The latter copies it from global to session env on new sessions. For copy bindings (vi mode example): bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy" With set -g mouse on, mouse selections will use copy-command automatically. This works because the subshell launched by copy-pipe/copy-command inherits the pane's environment, including $WAYLAND_DISPLAY.

Citations:


Runtime detection frozen at parse time due to double-quote expansion.

Line 40 uses $WAYLAND_DISPLAY inside double quotes in the tmux option value. Tmux expands $ENV variables during config parsing (not at runtime), so the clipboard backend selection locks to whatever environment was present when the tmux server started. This directly breaks runtime environment adaptation. Additionally, wl-copy and xclip should have command existence checks before use.

🔧 Suggested fix
-set -g `@clipboard_cmd` "command -v pbcopy >/dev/null 2>&1 && pbcopy || { [ -n \"$WAYLAND_DISPLAY\" ] && wl-copy || xclip -selection clipboard; }"
+set -g `@clipboard_cmd` 'if command -v pbcopy >/dev/null 2>&1; then pbcopy; elif [ -n "$WAYLAND_DISPLAY" ] && command -v wl-copy >/dev/null 2>&1; then wl-copy; elif [ -n "$DISPLAY" ] && command -v xclip >/dev/null 2>&1; then xclip -selection clipboard; else exit 1; fi'

Use single quotes to defer variable expansion until shell execution time, and add command checks for both fallback commands.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@home-manager/programs/tmux/tmux.conf` at line 40, The tmux option set -g
`@clipboard_cmd` currently uses double quotes so $WAYLAND_DISPLAY is expanded at
tmux parse time; change the value assigned by set -g `@clipboard_cmd` to use
single quotes so the shell evaluates $WAYLAND_DISPLAY at runtime, and update the
shell expression referenced by `@clipboard_cmd` to check for the existence of
wl-copy and xclip (e.g., using command -v wl-copy >/dev/null 2>&1 && wl-copy)
before falling back, ensuring each fallback has a command-existence test and
pbcopy remains checked first.

@shunkakinoki
shunkakinoki merged commit a9bdd1d into main Mar 20, 2026
40 of 43 checks passed
@shunkakinoki
shunkakinoki deleted the fix/tmux-cross-platform-clipboard branch March 20, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants