aamir jawaid

How TMux and Rex differ

With the latest release of Rex, I wanted to think through how it and T-Mux were different.

For this we first need to understand how TMux works. TMux is essentially a service that has two parts - one part for managing the actual programs and the other for painting the characters on the screen. Since these two parts are separate, your programs outlast your client being alive. The server is also responsible for remembering the state of the screen including the scrollback buffer for each program. Since TMux itself is just a terminal program which creates virtual panes for all its visible programs. It needs to manage the visuals of the screen as well so the server also needs to build a composition of what the screen looks like for the user based on the child programs’ outputs, and the various borders and other visual elements (fun fact, borders in TMux are just | characters). This forces is TMux to do two passes to show the final output:

  1. The first pass receives the output from the PTY of the actual program. In this first pass TMux is essentially building a copy of the output and storing the programs state.
  2. The second pass takes all the various outputs from all its visible programs and figures out exactly which character gets inserted for each available spot in the terminal window.

This has been an extremely common setup for developers for decades. The main issues with this are speed (since it’s managing a virtual layout at all times) and fighting the client terminal when it comes to things like selecting text (since the client terminal has no idea about physical borders between different program outputs).

Rex takes a slightly different approach. It owns both the service that manages the various programs and also the client that renders the output. Its client renders each visible program in a real libghostty backed terminal. So there is no virtual windows to manage. Because of this, it gets directly send bytes after the first pass to the client terminal. This results in performance gains and also getting all client terminal benefits for free since the output is being rendered in its own dedicated window inside the Rex client.

Additionally, having control over the client also means it can pioneer new protocols between its service and its client and the target programs can opt in to these protocols if needed. (They’re already doing it with protocols like session restore).