How to copy text to system clipboard in tmux?

Quick Answer

To copy text to the system clipboard in tmux, you need to configure tmux to integrate with your system clipboard. This typically involves entering copy mode (Ctrl+b [), selecting text, and using a modified copy command that pipes to your system's clipboard utility.

Ctrl+b+[then select text and pressy

Detailed Explanation

By default, tmux only copies text to its internal buffer, not to your system clipboard. To enable system clipboard integration, you need to add specific configuration to your tmux.conf file and possibly install additional utilities.

Step 1: Install clipboard utility (if needed)

  • macOS: brew install reattach-to-user-namespace
  • Linux: sudo apt install xclip or sudo apt install xsel
  • WSL: sudo apt install xclip and ensure Windows clipboard integration

Step 2: Configure tmux.conf

Add these lines to your ~/.tmux.conf file:

# For macOS

set-option -g default-command "reattach-to-user-namespace -l $SHELL"
# Enable vi mode
set-window-option -g mode-keys vi
# Setup 'v' to begin selection, 'y' to copy to system clipboard
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'

# For Linux with xclip

set-window-option -g mode-keys vi
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

Step 3: Use clipboard integration

  1. Press Ctrl+b [ to enter copy mode
  2. Navigate to text you want to copy
  3. Press v to start selection (if using vi mode)
  4. Use navigation keys to select text
  5. Press y to copy to system clipboard

Pro Tip: Mouse Selection

For easier copying, enable mouse mode and configure it to copy to clipboard:

# Enable mouse mode
set -g mouse on

# For macOS: Copy mouse selection to clipboard
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"

# For Linux: Copy mouse selection to clipboard
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"