11/24/2024 Tmux basics
tmux is a terminal multiplexer.
A practical example
Simply run tmux in your shell : you will enter a session.
Note that, you can still interact with the shell just like before.
So now we can experiment something to illustrate what it feels being in a session :
Let's run a dummy long-running program, like this :
sh -c 'while true; do echo "This is a long-running program"; date ; sleep 5; done'Let the long program run, but let's find a way to quit our terminal.
I suppose you can for example go and find your terminal icon, and right-click quit on it.
Then, let's open a fresh new instance of our terminal, and run this command :
tmux attachWe are back in the session,
and we can see that our long-running program did
run continuously during our absence, without any interruption.
Basic concept
When you enter a tmux session, you get a shell that is not bound to your current terminal instance.
Some interesting consequences :
- you can quit your terminal instance without interrupting the
session - you can join the
sessionfrom another terminal instance withtmux attach - you can work in the same
sessionon multiple terminal instances.
A common and useful case :
Let's say you are about to run a critical process on a remote machine you're connected with via ssh.
You should ensure that, if your network connection causes the ssh connection to fail,
your critical process keeps running safely on the remote machine.
For that, use tmux on the remote machine, and run your critical program inside a session.
Then if, by accident, you get disconnected from the remote machine,
you will sooner or later be able to ssh again into it.
And by running tmux attach, you'll see that your critical program kept running safely.
Learn to configure tmux rather than the terminal.
You may have seen that tmux comes with a default bar sitting at the bottom of the terminal.
In fact, tmux can offer more than just terminal multiplexing :
- you can create multiple
sessions, and switch between them. - you can open multiple
windowsin asession - you can open multiple
panesin awindow - you can fully customize the look of a
session( status-bar, window-bar, etc )
By learning a bit of tmux configuration, we get the opportunity to :
- use a lightweight terminal like
alacrittyand delegate totmuxall the multi-window stuff. - setup key-bindings, tools, plugins, a lot that a terminal alone cannot provide.
See documentation : Man tmux
Basic commands and key-bindings
tmux ls
tmux attach
tmux detach
tmux kill-sessionPro tip :
on macOS,
go to settings > Keyboard > Modifier Keys,
and remapCapsLocktoCtrl
On the keyboard, Ctrl-B is the default prefix to send commands to tmux.
Ctrl-b + c : create a new window in the session
Ctrl-b + p : go to previous window
Ctrl-b + n : go to next windowIn case you want to change this default prefix to e.g Ctrl-A,
create a file named $HOME/.tmux.conf with the following content :
unbind C-b
set-option -g prefix C-a
bind C-a send-prefixthen apply this new configuration :
tmux source ~/.tmux.confA simple configuration
TO DO ;)