Copy-pasting from WSL

bioinformatics
Author

Alexis Weinreb

Published

December 2, 2023

In my current setup, my desktop computer is running Windows, but most bioinformatics tools I need are best used from a Linux command line. In addition, I often run programs on a computing cluster. In practice, I use Windows Subsystem for Linux (WSL) on my computer, within the Windows Terminal, and, if needed, connect to the HPC cluster by ssh.

One operation is not as trivial as expected: copy-pasting.

Copying a local command

As an example, suppose I have a tsv file on my computer, and wish to copy-paste its contents into Excel.

echo -e "Some\tcolumn" > myfile.tsv
echo -e "1\t2" >> myfile.tsv

cat -A myfile.tsv
#> Some^Icolumn$
#> 1^I2$

My first idea is to use cat or less to display the file contents, use the mouse to select them, and use ctrl + shift + C to copy. This mostly works, but with some limitations. First, the tab characters are lost (are replaced by multiple spaces), second, this is unpractical when the file doesn’t fit on the screen.

Inspired by this post by Michael Kuhn, using pbcopy and pbpaste on a Mac, I went looking for the Windows version, which is to call CLIP.exe.

cat myfile.tsv | clip.exe

Then one can simply switch to Excel or another application and paste the content of the file

Bonus: pasting in the command line

To directly paste from a command, one can use (source):

powershell.exe -c Get-Clipboard

From a remote computer

This works great from a terminal on my own computer, but often I have files on a cluster, and want to copy some of it. Since the computing cluster is running its own Linux system, I can’t just access clip.exe on my computer.

The easiest solution seems to open a new terminal on my local computer and use ssh to run a single command remotely, copying its results:

ssh cluster "cat /path/to/file.tsv" | clip.exe

Alternatively, it is possible to open a reverse ssh connection from the remote cluster.