tmux is the modern version of screen in Linux.
tmux creates sessions (tmux server is doing this)
Each session can have one or more windows.
Each window can have one or more panes.
Each such pane is a virtual terminal on its own.
See the image on the left to see what's the setup of tmux.
tmux version
tmux -V
tmux prefix
tmux uses a prefix to initiate a tmux command; the default prefix is Ctrl-b. If you're a seasoned screen user you won't like this prefix. Luckily, you can change that in the ~/.tmux.conf file like so:
set-option -g prefix C-a
unbind C-b
The above two lines will change the prefix from the default Ctrl-b to the (screen default) Ctrl-a key combination. Since Ctrl-b is also unbound you can use it maybe for some other action in tmux.
Note: all similar commands below should also be put in the tmux configuration file ~/.tmux.conf, this will not be repeated.
Each time the tmux configuration file is changed, you must run the following command sequence when you're in a tmux session:
Ctrl-a : (the status bar will change colour and will become orange-ish)
source-file ~/.tmux.conf
Later on, we'll see how we can connect a shortcut in the configuration file so that you don't have to do the update anymore using the above command sequence.
You can also quit tmux and restart it again to re-read the configuration file but that's most of the time not really handy, especially if you have many windows and/or panes open.
tmux terminal
To avoid problems with other terminal types outside of tmux, make sure the tmux terminal is set to screen-256color or tmux-256color. You can set this in the configuration file like so (I've chosen for the tmux-256color setting):
set -g default-terminal "tmux-256color"
This will avoid issues with colours and keystrokes and so on.
Creating a window
Ctrl-a c
Creating a pane
Split window horizontally: Ctrl-a "
Split window vertically: Ctrl-a %
Needless to say those are "weird" keys but luckily again, this can be changed.
I'm now using (based on many preferences I've found on the web) the following key combinations:
Split window horizontally: Ctrl-a |
Split window vertically: Ctrl-a -
Looks more logically, right?
Starting window from 1 instead of 0
By default a window starts from 0. If you want to choose another window you can do so by typing Ctrl-a <window_number>.
However, 0 is not handy since it's on the other side of the keyboard. So, switching between window 0 and window 1 is traversing the keyboard.
You can change the default starting window number with the following command:
set -g base-index 1
From now on, the first window created will have the number 1, not 0. The following will have number 2 and so on...
Starting pane from 1 instead of 0
Same issue we have with panes: they also start with 0 instead of 1. To see the pane numbers, type the command Ctrl-a q. You will see for a short time the pane numbers: they will appear in colour and the first pane created will have the number 0.
To change this, give the following command:
setw -g pane-base-index 1
Next time the panes will start from 1 or if there are already panes present they will be renumbered and also start from 1.
Using the mouse
It's always handy to use the mouse, even in tmux. To activate the mouse, add the following line into the configuration file:
set -g mouse on
Easily reload a modified configuration file
Instead of having to type the following sequence to re-read the tmux configuration file:
Ctrl-a :
source-file ~/.tmux.conf
it's much more easy if we could connect this to a shortcut.
This can be done by adding the following line to the configuration file:
bind r source-file ~/.tmux.conf
This "binds" the key r to the command given. So, if the configuration file is change you only have to type Ctrl-a r to reload the content of the configuration file. Handy, huh?
Creating a pane
Split window horizontally: Ctrl-a "
Split window vertically: Ctrl-a %
Needless to say those are "weird" keys but luckily again, this can be changed.
I'm now using (based on many preferences I've found on the web) the following key combinations:
Split window horizontally: Ctrl-a |
Split window vertically: Ctrl-a -
Sounds more logically, right?
Swap panes
Prefix-} to swap pane from left to right or Prefix-{ to swap pane from right to left.
See an overview of windows and panes
To see how many windows are created and their "inside" panes, type Ctrl-a w. The current window will be split in two vertical windows and the top part will show the window(s) and the pane(s) open in the window.
The bottom part will show a kind of "screenshot" of the active pane(s) in a window.
When there's a "+" in front of a line, press the right-arrow key to expand. Once expanded, a "-" will be shown.
To collapse the section again, press the left-arrow key.
Travers to another window and/or pane using the up- and down-arrow keys.
Status bar
tmux comes with a default status bar. The information which is shown on the status bar is from left to right:
Session (starts with 0 unless a name was given to the session while creating it)
Windows (starts with 0 unless you've told tmux to start with 1: see item above)
Time (HH:MM) on the right side
Date (DD-MM-YYY) on the right side
This can all be changed drastically with lots of existing plugins and so on but I won't go into detail here since that would bring us way too far off-topic.
There are, however, a few small changes I've done to my status bar by adding the following two lines into the configuration file:
set -g status-right '%H:%M-%d/%m/%y'
set -g status-interval 1
The above two lines to the following:
on the right side of the status bar, I've changed a bit the way time and date are represented
the default interval time is 15 seconds but seen the fact I'm also showing the seconds I've changed the update interval to 1
MobaXterm is one of the many Linux-alike tools that can be used on Windows to do ssh sessions and the like to remote devices (e.g. Raspberry Pi). Another very important feature of MobaXterm is that it has an X11 server as standard server.
Some key features:
Embedded X server
Easy DISPLAY exportation
X11 forwarding capabilities
Tabbed SSH sessions terminal (based on PuTTy)
Many Linux commands available in Windows
...
Why did I choose for MobaXterm while I also have WSL2 active? Well, it's all because of immediateC I'm currently testing out for my home automation system. immediateC has applications written in Perl/Tk and some of the functionality isn't working fine when running under WSL2. One of them is the File and Build menu that doesn't work fine at all and that's in fact a must.
MobaXterm doesn't have this issue so for this particular situation I'm using it above using WSL2.
Next to this, when using Visual Studio Code in WSL to remotely write code on both my Raspberry Pi's for my home automation system it interferes with the Perl/Tk GUI too. I do sometimes see completely screwed Perl/Tk UI's that are totally unworkable. Also other applications like my web browser are affected by the Visual Studio Code app after some time of working with it.
Otherwise, WSL2 is definitely my main app.
There's a free and a paid version of MobaXterm but in many - if not all - cases the free version is more than sufficient for hobbyists.
To install, go to the MobaXterm website and download MobaXterm for your OS.
Issue with temp directories
By default, root and home are temp directories. When MobaXterm ends, all your stuff is lost. So, when installing a package, e.g. tmux, and you quit MobaXterm , you have to reinstall it again when launching MobaXterm.
Persistent root and home
To avoid this, make the temp and root directory permanent through the Settings -> General menu in mobaxterm. Create somewhere a MobaXterm directory on your harddisk and point this as the persistent root.
There will be a subdirectory created called slash and in there you will have the Linux RFS mounted. It will have all the normal Linux directories you find in a Linux RFS, among others, the home directory.
If you then install packages or you download other stuff in the home directory, they're persistent in the RFS.
If you have the same setup over and over again when tmux starts, you can pull everything in a bash script and call the bash script to launch tmux for you with all the settings pre-registered.
An example of such bash script is given below. It opens 3 tmux sessions and in every session one or more windows are created. In those windows one or more panes are created with specific properties (e.g. predefined horizontal and/or vertical with). One of the panes also launches a htop application.
Finally, the cursor is moved to the first window and first pane within that window created.
Here's the script tstart.sh:
#!/bin/bash
echo "Starting predefined tmux session"
tmuxstart1() {
tmux new-session -d -s sess >/dev/null
tmux rename-window -t sess:1 'main'
tmux splitw -v -p 10 -t sess:1.1
tmux splitw -h -p 80 -t sess:1.2
#required; otherwise pane numbering is bs
tmux select-pane -t sess:1.1
tmux splitw -h -p 5 -t sess:1.1
tmux send-keys -t sess:1.3 'htop' Enter
tmux send-keys -t sess:1.2 'tmux clock -t sess:0.1' Enter
tmux select-pane -t sess:1.1
tmux new-window -t sess
tmux rename-window -t sess:2 'second'
tmux splitw -v -p 10 -t sess:2.1
tmux splitw -h -p 80 -t sess:2.2
tmux select-pane -t sess:2.1
tmux splitw -h -p 5 -t sess:2.1
tmux clock -t sess:2.2
tmux new-window -t sess
tmux rename-window -t sess:3 'scratch'
tmux splitw -v -p 10 -t sess:3.1
tmux select-pane -t sess:3.1
tmux splitw -h -p 5 -t sess:3.1
tmux clock -t sess:3.2
tmux select-window -t sess:1.1
tmux a -t sess
}
tmuxstart
When you call the script tstart.sh it will run all the above commands.