1

Not sure if mine is a weird/unusual workflow, but I have a Windows machine, where I use Windows Terminal. I usually ssh into a remote server, using WSL on Windows Terminal. The remote server is running RHEL.

Now I'm having an issue trying to copy to clipboard on the remote server using xclip. xclip always returns Error: Can't open display. Even if I manually set export DISPLAY=:0 it would still give me the same error. I also set ForwardX11 yes in ~/.ssh/config but that didn't help either.

The reason I need this is that ultimately I would like to enable select-and-copy in tmux, on the remote server. The .tmux.conf would look like something like this:

set-option -s set-clipboard off
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip'
bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip'

On WSL itself I would just replace 'xclip' with clip.exe and it would work. But since this is a remote server running running in Windows Terminal, all Windows executables are not available.

Does anyone know how I could enable this? Thanks in advance!

4
  • You should be able to open up a remote text editor and use WSL to paste your text inside the file.
    – harrymc
    Feb 17, 2023 at 20:11
  • @harrymc sorry I don't quite understand, could you please elaborate? My main use case is to select & copy text in terminal, in tmux, inside the remote server. And since I'm on a remote (headless?) server, xclip can't seem to find the correct $DISPLAY. Even if I edit some file using vim/nvim and yank some text using y, it would still give me the error clipboard: error invoking xclip: Error: Can't open display: :0.
    – rollschild
    Feb 17, 2023 at 20:21
  • But, using the normal tmux way does work: hold shift and select some text using your mouse and copy it using Ctrl + Shift + C. I'm not an expert on tmux so I don't know whether xclip is involved in this process or why it works. But I'm trying to look for a way with fewer steps that would mirror my workflow on WSL or other Linux distros - select a text using your mouse and the selected text will be automatically copied to clipboard.
    – rollschild
    Feb 17, 2023 at 20:26
  • Linux has more than one clipboard. You don't have the graphical clipboard in your environment, so you may either use the method in your last comment or use the Windows clipboard through WSL to update your files. If this doesn't help you, please give more details why.
    – harrymc
    Feb 17, 2023 at 20:31

1 Answer 1

1

Ok I figured it out. It turned out to be quite easy - Always read the docs!

So, xclip relies on X server to be running, which is not the case on my remote RHEL server. One simple way to check if X is running is to echo $DISPLAY and see if it is set. But this doesn't always work afaik. Another way to check is ps -e | grep X . Either way, for my case, X is not running so xclip wouldn't work.

But, the tmux doc actually does talk about clipboard stuff. Simply follow the doc and I was able to fix it. The important thing to notice is you should use the set-clipboard option/method, not the Piping to an external tool like xsel one, since X server isn't running.

To summarize, read the tmux doc here and choose the OSC 52 and the set-clipboard option. Here is the related part in my .tmux.conf:

set -as terminal-features ',xterm-256color:clipboard'
set-option -s set-clipboard on

But yanking to clipboard still does not work inside Vim, since by default Vim tries to copy that to clipboard using xclip. But for now my problem is solved good enough.

EDIT:

The end result of this is, in tmux within my remote server, I can just select some text using my mouse, like press the mouse and drag to select a few words then release the mouse, and my selection will be automatically copied. I can then Ctrl + Shift + V to paste it somewhere else. No need to hold on Shift while selecting the text.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .