First what is a MUX? A MUX is a shorthand for Multiplexer which is used for selecting data commonly in electronics. TMUX is a Terminal Multiplexer. This means it selects Terminals to act on, as well as managing them into panes so you can easily access them. They also save the terminals in sessions, sessions can be thought of like your workspace. This means it can be used for really a lot of things actually. I use it for hosting servers without forking the process or for having processes run in the background in a tmux session rather than in a window(typically this is done with ncurses based applications as they display in your terminal). Another advantage is you can use it with vim or neovim(probably emacs too), so you can create a workspace for doing your text editing activities. In this I will provide examples of some scripts I have written using tmux, as well as some advantages. I won't be going over common keybinds and how to use tmux as a user, so if you're a newb to tmux, take some time to look for examples of using it. If you use tiling window managers, but haven't used tmux, you will feel right at home with tmux.⮩ This blog is a very good jumping off point into using tmux
One of the TUI(Terminal User Interface) applications I enjoy thoroughly is cmus(A music player written in C and NCurses). There's a lot of reasons to like cmus, it's easy to use, it can be used on a network, it can be controlled remotely with cmus-remote(which I use with my keybindings), however for all these advantages, it's not a daemon so it doesn't run in the background. A different music player like mpd might be suitable for this, but I chose to stick with cmus and write a script with tmux. Below is the script
tmux has-session -t cmus 2>/dev/null if [ $? != 0 ]; then tmux new-session -d -s cmus \; \ send-keys 'cmus' C-m\; fi tmux attach-session -t cmus
What this does it firstly checks if it has a session named cmus, and writes the result to to /dev/null. I then do a check on the result of the last command's output ($? is a mysterious variable that is the output of the last command you wrote). I check it's not 0, a 0 would mean it has a session and I should attach to it. If it's not a 0, I create this session. The -d flag being to detach after creating it. The -s flag being the session name. You will observe I split this over multiple lines, when a tmux session is made I can send it keys, with send-keys. So I tell it to stard cmus, and C-m(This enters). This starts a Tmux session with cmus running in it, detached. The final line is to attach to the session cmus(you can omit this if you want to manually attach to the session. I keep it because I use this with a keybind of mine to bring cmus up when I want to listen to music).
Read the above example, and you will learn that the first advantage of tmux is being able to run, detach and attach sessions, as well as send arbitrary key inputs to run commands accordingly. I would use this to run scripts in your $PATH.
But what if you want to have windows? I will ignore this for vim, because this has windows by default, you can look that up yourself, and you will find out about it, but some applications you want to do windows. For a short time I used to use a task manager called taskwarrior for doing todo lists(I don't anymore, I use a physical journal, but the code I wrote is still applicable here).
tmux has-session -t tasks 2>/dev/null if [ $? != 0 ]; then tmux new-session -d -s tasks \; \ send-keys 'while true;do clear; task priority limit:3;sleep 5;done' C-m\; \ split-window -v -p 75 \; \ split-window -h -p 30 \; \ send-keys 'while true;do clear; task burndown.daily;sleep 5;done' C-m\; \ select-pane -t 1 \; fi tmux attach-session -t tasks
What I did in the cmus example is repeated here and built upon. I run some commands in a loop(could be replaced with a script or executable of your choice. Maybe a crypto price watcher by curling rate.sx?). So I split the window vertically by 75%, then I split the window horizontally by 30%. This will now select the 1st pane as where you handle inputs, while the 2nd pane and 0th panes are handling your scripts. What this allows is running a lot of different applications in a very specific window layout(almost like a tiling window manager...?).
I have exhausted my supply for tmux examples that I wish to demonstrate, but you can see that if you make a script with a case statement for different tmux layouts this could be useful for some people. I will personally recommend looking at the man page for tmux to see more ways this can be used, because I have barely scratched the surface.
Anyway, other uses include when you ssh into a remote server. Why? Because if you lose connection, your terminal state isn't saved normally, however if you lose connection in a tmux session, you can just reconnect and reattach to the tmux session to pickup where you were before.
I hope these examples have illuminated why I would suggest using tmux for automating your workspaces. In some cases it's not worth this effort, but for others it is. I will let you dear reader, determine what is and isn't worth the effort to apply tmux in.
Written Feb 18(Thursday), 2021