jasonwryan.com

Miscellaneous ephemera…

Copy & Paste in tmux

image

I’ve posted a couple of times1 about tmux, the brilliant terminal mutliplexer. One question I see raised a lot in #tmux is how to copy and paste.

The documentation for tmux is excellent: the man page one of the most clear and thorough I have read. And while it is explained there, I thought I would share my approach anyway.

First, I set tmux to use vi keys:

1
setw -g mode-keys vi

Then I change the default keybinds to suit my workflow. As explained in the man page, the default keys to enter and exit copy mode are [ and ] respectively. I reset those in my .tmux.conf to more intuitive and Vim-like keys:

1
2
3
4
5
6
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

This seems a lot closer to all of the other Vim-like apps that I run - where Escape changes the mode, v begins visual selection, y yanks text to the buffer and p puts text copied from the buffer.

So, to string it all together, the command sequence necessary to copy some text from tmux and paste it is now (I bind my prefix to t):

Ctrlt,Escape   # enter copy mode
# move cursor to the start or end of the desired text string
v                        # to activate highlighting
# move cursor to cover the requisite string
y                        # to capture the string
q                        # exit copy mode
Ctrlt,p       # put/paste the text in the desired location

Simple.

This means that you can scroll back through the output of anything in your terminal, and copy and paste it anywhere. Once you set it up, it is a very powerful tool…

Notes

1.tmux - Terminal-multiplexer & Sessions in tmux

Comments