Reattaching tmux to your Xorg session

It happens infrequently enough to not investigate too much further, but sometimes I'll end up with my Xorg/X11 session crashing, or something else happening which leads to my existing tmux sessions failing to interact with the new X session, with errors like:

% gh repo view --web
Opening ... in your browser.
Error: cannot open display: :0

% xsel
xsel: Can't open display: (null)
: Connection refused

In this case, the existing panes/windows may end up seeing a related error about failing to connect to the display, but any new panes/windows created will be able to interact as expected.

This is a bit of an annoying one, but one way we can solve this is to nudge these panes to use the new DISPLAY. In a new pane/window/terminal session, check the DISPLAY environment variable:

% echo $DISPLAY
:1

That's likely different to what it shows in one of those existing panes - if so, run:

% export DISPLAY=:1

After which, you can then run commands that interact with your X session.

It's possible to run:

% tmux set-environment DISPLAY :0

Which will update all new panes - but as mentioned, tmux likely already handled that for you, as DISPLAY is one of the environment variables it updates, as per:

% tmux show-options -g update-environment
update-environment[0] DISPLAY
...

Instead of doing this by hand, it's also possible (Via) to script this to ??.

Written by Jamie Tanna's profile image Jamie Tanna on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

This blog post is 100% human-written, and has nothing derived from LLMs (be it code, editorial support or prose).

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.