Skip to content

Instantly share code, notes, and snippets.

@unphased
Last active March 11, 2022 00:08
Show Gist options
  • Save unphased/4455c734a30c2ab2ac2331d3c8becae0 to your computer and use it in GitHub Desktop.
Save unphased/4455c734a30c2ab2ac2331d3c8becae0 to your computer and use it in GitHub Desktop.
Copying content out of tmux/vim in terminal

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!

Outside tmux (plain terminal)

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.

SSH in plain terminal

Everything works the same as above.

tmux pane

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.

tmux over SSH

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.

SSH in tmux

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.

Summary of tmux clipboard capabilities

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.

From now on we will enable the mouse in tmux in demos, because this is a much more usable and practical UX. I included the procedures above to demonstrate the behavior seen from a pristine tmux.

Copying data out of vim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment