Mastodon hachyterm.io

A few days ago, I wanted to copy text from my tmux shell.

Unfortunately, the defaults from your standard terminal and shell (kitty and fish) don’t work, as tmux has its own key bindings.

I came across an excellent blog post called Everything you need to know about Tmux copy paste - Ubuntu, which listed all the steps you need to take.

First, the default behavior:

  1. Enter ‘copy mode’ by pressing CTRL+b, [.
  2. Use the arrow keys to go to the position from where you want to start copying. Press CTRL+SPACE to start copying.
  3. Use arrow keys to go to the end of text you want to copy. Press ALT+w or CTRL+w to copy into Tmux buffer.
  4. Press CTRL+b, ] to paste in a possibly different Tmux pane/window.

Please note that CTRL+B is the default tmux “prefix”. You can adjust it to a different key combination, if you want 1.

But you can customize your tmux configurations (works for tmux 2.4+):

## ~/.tmux.conf

## Use vim keybindings in copy mode
set-option -g mouse on
setw -g mode-keys vi
set-option -s set-clipboard off
bind P paste-buffer
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X rectangle-toggle
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel 'xclip -se c -i'
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -se c -i'
  1. Now you can enter copy mode normally with CTRL+B and [.
  2. Navigate the copy mode with vi-like-key bindings (u for up, d for down, etc.)
  3. Hit v to start copying.
  4. Press y or Enter to copy the text into the tmux buffer. This automatically cancels copy mode.
  5. Paste into the buffer with <prefix>+P (make sure that it’s uppercase P).

Or alternatively, use the mouse to copy text after you’ve entered copy mode.

The above commands use xclip, a Linux command line tool for X11. You can replace xclip -se c -i with a platform-specific command like pbcopy (MacOS) or wl-copy (Wayland).

Further Reading