This gist is written to address challenges related to copying data out of buffers that you may encounter when using tools in the terminal, in particular in dealing with modern versions of tmux
and vim
. Care is taken to use these tools with their configuration that is provided out of the box.
Couple of notes:
- All of this applies across Linux and macOS.
- Note that the "copied to clipboard" notification you will see in demos below is being generated by the Clipboard Indicator GNOME extension tool. It is useful to demonstrate as it happens when content is successfully preserved into our system clipboard. It also offers very useful clipboard history management, although many other apps can be used for the same purpose. For example, on macOS, I prefer to use the clipboard manager that is built into BetterTouchTool.
Let us proceed!
Click+drag to select content. Your terminal emulator will either implicitly grab what you selected, or you can Ctrl+C or Ctrl+Shift+C after you create the selection.
Gnome Terminal:
2022-03-10.18-06-27.mp4
Copying a selected region is a manual operation in GNOME Terminal.
Alacritty:
2022-03-10.18-18-43.mp4
Copy is implicit and occurs when you release the mouse drag. This is the case in many terminal emulators, such as PuTTY on Windows, etc.
For the rest of the demos I will only use alacritty.
Everything works the same as above.
Raw unconfigured tmux behavior:
2022-03-10.18-12-50.mp4
Ctrl+b, [ to enter "copy mode". Arrows and page up/dn are used to navigate in copy mode, and content is copied via the use of space to start and return to complete it.
The reason this works is because tmux's default configuration enables the set-clipboard
setting to on
, and this uses an xterm sequence which the terminal is able to understand and it will set your system clipboard.
Let's check. I'll set set-clipboard
to off:
2022-03-10.18-41-38.mp4
Notice how the clipboard did not get set when I hit Enter this time.
Let's explore a bit. If an xterm sequence is being used to transport the copied content, then this should work over SSH as well.
2022-03-10.18-43-25.mp4
Here I ran an unconfigured tmux inside an SSH session, performed a tmux copy-mode copy, we saw that my host machine received the content in its clipboard, and I exited the tmux and the SSH session and opened up a vim and demonstrated the contents of the system clipboard.
This is not any different than the situation of running SSH inside a raw terminal. An SSH session inside a tmux pane is the same as a regular tmux pane.
These cases are enough to demonstrate that tmux's built-in copy-mode behavior works in an ideal way, as the xterm method will work regardless of the degree to which SSH and tmux are nested, as the escape sequence will tunnel through just the same as the rest of the content being displayed to you. if you are having trouble with your clipboard, look into the settings specific to the terminal emulator that you're using.