How to Copy Text in Tmux to System Clipboard

Tmux (Terminal Multiplexer) adalah tool CLI yang memungkinkan Anda menjalankan dan mengelola beberapa sesi terminal dalam satu jendela terminal. Tool ini sangat berguna untuk meningkatkan produktivitas, terutama saat bekerja dengan server remote atau menangani banyak task sekaligus.

Dalam panduan ini, kita akan menginstal dan mengonfigurasi Tmux agar dapat menyalin teks yang dipilih ke system clipboard.

Install

Berikut cara instalasi tmux sesuai OS yang digunakan.

Linux DEB

apt -y install tmux xclip

Linux RPM

dnf -y install tmux xclip

MacOS

brew install tmux

Configure

Buat file .tmux.conf pada HOME direktori.

nano ~/.tmux.conf

Selanjutnya tambahkan konfigurasi sesuai OS yang digunakan.

Linux

set -g mouse on
set -g history-limit 40000
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down

# To copy, left click and drag to highlight text in yellow,
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -sel clip"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -sel clip"

# color
set -g pane-border-style fg=red
set -g pane-active-border-style fg=red

# term
set -g default-terminal "xterm-256color"
bind-key -n Home send Escape "OH"
bind-key -n End send Escape "OF"

# status bar
set -g status-left ''
set -g status-right-length 0

# Ensure window index numbers get reordered on delete.
set-option -g renumber-windows on

# Start windows and panes index at 1, not 0.
set -g base-index 1
setw -g pane-base-index 1

MacOS

set -g mouse on
set -g history-limit 40000
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down

# To copy, left click and drag to highlight text in yellow,
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"

# color
set -g pane-border-style fg=red
set -g pane-active-border-style fg=red

# term
set -g default-terminal "xterm-256color"
bind-key -n Home send Escape "OH"
bind-key -n End send Escape "OF"

# status bar
set -g status-left ''
set -g status-right-length 0

# Ensure window index numbers get reordered on delete.
set-option -g renumber-windows on

# Start windows and panes index at 1, not 0.
set -g base-index 1
setw -g pane-base-index 1

Simpan konfigurasi, dan Tmux siap digunakan.

Referensi: