Everything you need to know about Tmux copy paste - UbuntuJun 16, 2016
Copying from a Tmux session is something every Tmux user struggled with once. I’m listing out all the stuff I learnt in this blog.
I tested everything on Ubuntu 14.04 Trusty Tahr, which runs Tmux version 1.8.
To check your Tmux version, run
tmux -V. If you have a Mac, see
Know about copy buffers
When you do a
c, the stuff you copy is stored in your computer’s
buffer, called ‘clipboard’ from where you can paste anywhere by doing a
CTRL+‘v’. Tmux has it’s own buffer for coppying, which we’ll
call ‘tmux buffer’. Our goal is to understand in a Tmux session how to copy to
tmux buffer, and also to clipboard.
You can always copy stuff into clipboard while usin Tmux. “Why do I need a Tmux
buffer then”, you might wonder. This is because, in your shell, the text you
want to select might not fit in your current screen (e.g. output of
/etc/passwd file). If you copy normally, you will only be able to copy text
visible on your screen, and not the output which is ‘scrolled up’ due to a lot
Tmux copy-paste - the defaults
The defaults are slighly involved, so this section is purely for informational purposes, and shouldn’t be memorized. Skipping this section is perfectly okay.
- Enter ‘copy mode’ by pressing
- Use the arrow keys to go to the position from where you want to start copying. Press
SPACEto start copying.
- Use arrow keys to go to the end of text you want to copy. Press
wto copy into Tmux buffer.
]to paste in a possibly different Tmux pane/window.
Tmux Vim-bindings for copying into tmux buffer
Adding configuration described in this section will give you easier shortcuts for copy-pasting in Tmux. Moreover, these shortcuts work very similar to Vim’s copy-pasting shortcuts!
Add these lines in a file by name
bind P paste-buffer bind-key -t vi-copy 'v' begin-selection bind-key -t vi-copy 'y' copy-selection bind-key -t vi-copy 'r' rectangle-toggle
Now you can enter copy mode by pressing
[, and then go to start point, press ‘v’ and start copying. After you have selected text you want to copy, you can just press ‘y’ (or the default ‘enter’ key) to copy the text into Tmux’s buffer. This is exactly the commands you would use in Vim to copy text.
To paste, press
P. Note that it’s capital ‘p’ (i.e.
p). This again is similar to Vim’s shortcut ‘p’ for paste, though not exactly similar. You’ll realize in your Tmux journey why didn’t we use a small ‘p’ instead of a capital ‘P’ ;)
Copy from tmux buffer to system buffer (clipboard)
For this to happen, you need to install
xclip on your computer. Do it as:
sudo apt-get install --assume-yes xclip
After that, you need to add this line in
bind -t vi-copy y copy-pipe "xclip -sel clip -i"
Now close all your tmux sessions. From now onwards, whatever you copy in Tmux buffer will also land into system clipboard.
Tmux copy with mouse drag!
You can enable ‘mouse mode’, using which you can copy text into tmux buffer by
mouse drag. For doing that, you just need to add this line to your
setw -g mode-mouse on set -g mouse-select-window on
But now I can’t do normal copy-paste with mouse!
You’ll notice that now all your selections will go to tmux buffer, and not clipboard buffer. Of course, you can enable copying to system clipboard as described in a section above. However, you can notice that you can’t double click to select a complete word with vi’s tmux copy-pasting shortcuts + mouse option enabled.
SHIFT button when copying, and now you can copy as if Tmux
doesn’t exist! :)
Copy from a remote server
xclip on the remote Ubuntu/Linux server, and add the line mentioned
bind -t vi-copy y copy-pipe "xclip -sel clip -i") to
that server. Also, pass
-X option when making SSH connection to the server,
ssh -X remoteuser@remotehost
And after that everything you copy into remote’s Tmux buffer will get copied over to local clipboard. Magic!
Done! Don’t forget to comment if you know something worth letting everybody know! Thank you:)