Post

Tmux - Copy Paste Configuration

Tmux - Copy Paste Configuration

Mastering tmux Copy & Paste on Ubuntu 26: A Complete Setup Guide

If you’ve ever been frustrated by tmux copying text from the wrong pane — or copying nothing at all — this guide is for you. We’ll walk through configuring tmux to copy only from the selected pane and wire it up to Ubuntu’s system clipboard so Ctrl+V works everywhere.


Prerequisites

  • Ubuntu 26.04
  • tmux installed (sudo apt install tmux)
  • A terminal emulator (GNOME Terminal, Kitty, Alacritty, etc.)

Verify your tmux version (should be 3.x+):

1
tmux -V

Step 1: Install xclip

xclip bridges tmux’s internal buffer and the system clipboard.

1
2
sudo apt update
sudo apt install xclip -y

Verify the install:

1
xclip -version

Step 2: Create or Edit Your tmux Config

Open (or create) the tmux configuration file:

1
nano ~/.tmux.conf

Step 3: Add the Full Configuration

Paste the following into ~/.tmux.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# ─── Mouse & Key Mode ─────────────────────────────────────────────────────────

# Enable mouse support
set -g mouse on

# Use vi-style keys in copy mode
setw -g mode-keys vi

# ─── Pane-Isolated Selection ──────────────────────────────────────────────────

# Prevent selection from bleeding across panes
set -g focus-events on

# Do not auto-copy on mouse release (we handle it manually below)
unbind -T copy-mode-vi MouseDragEnd1Pane

# ─── System Clipboard Bindings ────────────────────────────────────────────────

# Copy to system clipboard on mouse drag end
bind -T copy-mode-vi MouseDragEnd1Pane \
  send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"

# Copy to system clipboard when pressing Enter in copy mode
bind -T copy-mode-vi Enter \
  send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"

# Copy to system clipboard when pressing 'y' (yank) in copy mode
bind -T copy-mode-vi y \
  send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"

# ─── Paste from System Clipboard ──────────────────────────────────────────────

# Ctrl+b P to paste from system clipboard
bind P run "xclip -selection clipboard -out | tmux load-buffer - && tmux paste-buffer"

# ─── Optional: Better Visual Selection ───────────────────────────────────────

# Start visual selection with 'v' (like vim)
bind -T copy-mode-vi v send-keys -X begin-selection

# Rectangle/block selection with Ctrl+v
bind -T copy-mode-vi C-v send-keys -X rectangle-toggle

Step 4: Reload the Configuration

Apply changes without restarting tmux:

1
tmux source-file ~/.tmux.conf

Or if you’re outside a tmux session:

1
tmux start-server && tmux source-file ~/.tmux.conf

How to Use It

Method 1: Mouse Selection (Easiest)

  1. Click inside any tmux pane to focus it
  2. Hold and drag to select text — only that pane’s text is selected
  3. Release the mouse — text is automatically copied to the system clipboard
  4. Paste anywhere with Ctrl+V

Method 2: Keyboard Copy Mode (Precise)

  1. Press Ctrl+b [ to enter copy mode
  2. Navigate with arrow keys or vi keys (h j k l)
  3. Press v to start selection
  4. Move to end of desired text
  5. Press y or Enter to copy to system clipboard
  6. Press Ctrl+b P to paste back into tmux, or Ctrl+V anywhere else

Troubleshooting

“xclip: Can’t open display”

This happens if you’re running in a headless or Wayland-only environment.

Fix for Wayland: Replace xclip with wl-copy:

1
sudo apt install wl-clipboard -y

Then update your ~/.tmux.conf, replacing every occurrence of:

1
xclip -selection clipboard -in

with:

1
wl-copy

And for paste, replace:

1
xclip -selection clipboard -out

with:

1
wl-paste

Text still copies from wrong pane

Make sure you click to focus the target pane first before selecting. You can add a visual indicator for the active pane:

1
2
3
# Highlight active pane border
set -g pane-active-border-style 'fg=colour51,bold'
set -g pane-border-style 'fg=colour238'

Copy mode exits before I finish selecting

Make sure this line is not in your config:

1
set -g aggressive-resize on   # unrelated but can cause issues

And confirm you’re using copy-pipe-and-cancel (not just copy-pipe) for clean exits.


Quick Reference Card

ActionShortcut
Enter copy modeCtrl+b [
Start selection (vi)v
Block selectionCtrl+v
Yank / copyy or Enter
Exit copy modeq or Escape
Paste from clipboardCtrl+b P
Scroll up in copy modeCtrl+u
Scroll down in copy modeCtrl+d

Final ~/.tmux.conf at a Glance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
set -g mouse on
setw -g mode-keys vi
set -g focus-events on

unbind -T copy-mode-vi MouseDragEnd1Pane
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"
bind -T copy-mode-vi Enter             send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"
bind -T copy-mode-vi y                 send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -in"
bind -T copy-mode-vi v                 send-keys -X begin-selection
bind -T copy-mode-vi C-v               send-keys -X rectangle-toggle
bind P run "xclip -selection clipboard -out | tmux load-buffer - && tmux paste-buffer"

set -g pane-active-border-style 'fg=colour51,bold'
set -g pane-border-style 'fg=colour238'

That’s it! You now have a tmux setup where:

  • ✅ Selection stays within the focused pane
  • ✅ Copied text lands in Ubuntu’s system clipboard
  • ✅ Both mouse and keyboard workflows are supported

Happy multiplexing! 🖥️

This post is licensed under CC BY 4.0 by the author.