tmux clipboard integration is surprisingly difficult because tmux is designed to be a self-contained terminal multiplexer, not an intermediary for system-wide clipboard operations.
Here’s how you can see it in action, and then we’ll break down why it’s complicated and how to solve it.
Imagine you’re in a tmux session, working on a remote server. You’ve got a long command output you need to copy.
# Inside tmux session
ls -l /usr/share/doc
You select the output, but when you try to paste it into your local editor, it’s empty or contains garbage. This is the problem we’re solving.
The core issue is that tmux itself doesn’t have direct access to your operating system’s clipboard. It operates within its own isolated environment. When you "copy" text within tmux using its built-in copy mode, that text is stored in tmux’s internal buffer, not your system’s clipboard. To get it out, you need a bridge.
The most common and robust solution involves xclip or xsel on Linux/BSD systems, or pbcopy on macOS. These are command-line utilities that interact with the X Window System (or Wayland) clipboard or the macOS pasteboard.
1. Installing the Clipboard Tools
First, ensure you have the necessary tools installed on the client machine (where your terminal emulator is running).
- Debian/Ubuntu:
sudo apt update && sudo apt install xclip - Fedora/CentOS/RHEL:
sudo dnf install xclip - macOS:
pbcopyis built-in.
2. Configuring tmux
You need to tell tmux to pipe its copy buffer to these external tools. This is done in your ~/.tmux.conf file.
Add these lines to your ~/.tmux.conf:
# Copy mode binding to copy to system clipboard
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"
bind-key -T copy-mode-emacs y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"
# For macOS:
# bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
# bind-key -T copy-mode-emacs y send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi y: This binds theykey invimode withintmux’s copy mode.send-keys -X copy-pipe-and-cancel: This is thetmuxcommand to send keys.copy-pipe-and-cancelmeans it will execute a command, pipe the selected text to it, and then exit copy mode."xclip -selection clipboard": This is the command that receives the piped text.xclipis invoked with-selection clipboardto target the primary clipboard (the one you typically use with Ctrl+C/Cmd+C and Ctrl+V/Cmd+V). If you want to use the secondary (middle-click paste) selection, usexclip -selection primary.
After saving ~/.tmux.conf, reload your tmux configuration:
# Inside tmux
tmux source-file ~/.tmux.conf
Or, restart your tmux server.
3. How it Works (The Mental Model)
When you enter tmux’s copy mode (e.g., Ctrl+b [), navigate to the text you want, and press y (in vi mode), tmux does the following:
- It captures the selected text.
- It executes the command specified in
copy-pipe-and-cancel(xclip -selection clipboard). - It pipes the captured text directly to the standard input of that command.
xclipreceives this text and places it onto the X selection buffer.tmuxexits copy mode.
Now, when you switch to another application on your local machine and press Ctrl+V (or Cmd+V), the application requests the content of the clipboard from the operating system, which xclip populated.
4. Troubleshooting Common Issues
xclip: command not found(or similar): This meansxclipisn’t installed on your local machine, or it’s not in yourPATH. Install it using your package manager.- Copying works, but pasting is empty/garbage:
- Remote vs. Local: Double-check that
xclipis installed and configured on your local machine, not the remote server you’re SSH’d into. Thecopy-pipecommand runs on the client side wheretmuxis executing. - SSH X11 Forwarding: If you’re using SSH and copying from a remote
tmuxsession to your local machine, you might need X11 forwarding enabled (ssh -Xorssh -Y). However, thecopy-pipemethod bypasses the need for X11 forwarding between the remote server and your local machine for the clipboard data itself, becausetmuxon the client is piping to a localxclip. The issue is usually thatxclipisn’t installed locally, or thetmux.confis on the remote machine instead of the local one. - Wayland: If you’re on a Wayland session,
xclipmight not work directly. You might needwl-copy(fromwl-clipboard). Thetmux.confwould then look like:
Install it withbind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy" bind-key -T copy-mode-emacs y send-keys -X copy-pipe-and-cancel "wl-copy"sudo apt install wl-clipboard(Debian/Ubuntu) orsudo dnf install wl-clipboard(Fedora). - Tmux Configuration Not Loaded: Ensure you reloaded your
tmuxconfig (tmux source-file ~/.tmux.conf) or restartedtmux. - Incorrect Key Binding: Verify the key binding in
~/.tmux.confmatches your copy mode style (vi or emacs).
- Remote vs. Local: Double-check that
- Copying large amounts of data: If you’re copying gigabytes of data, this method might be slow or hit buffer limits. For very large data transfers, consider alternative methods like
scp,rsync, or screen sharing.
The most overlooked aspect is often that the copy-pipe command executes on the client machine. If your tmux.conf is configured on the remote server, it’s trying to pipe to xclip on the server, which doesn’t help your local clipboard. Ensure your ~/.tmux.conf is active for your local tmux sessions.
The next hurdle is often configuring mouse support within tmux to select text more intuitively.