Tmux - fun with windows and sessions
From the article Tmux - fun with terminal splitting we already know how we can split the terminal into several pseudo-terminals and run processes in them or simultaneously display information. This time we will take care of the windows and the basic functions of the session in Tmux. In fact, when we split the terminal in the previous article, all these splits were made within one window with the number 0 and one session named 0. I'm curious if you noticed it in the bottom left corner of the terminal if not, I will show you it soon again.
Let's fire up the tmux and see if that's true.
tmux
It's like I said. Yay me :)
Let's try to create another window, and on this occasion we will learn how to do it. As we remember, the most important shortcut in tmux is control + b which will be necessary for this. So, let's create a new window using the following key combination:
control + b and c
And what does our tmux look like right now?
As we can see, we have 2 windows with numbers 0 and 1. The active window is marked with an asterisk. Now we will learn how to switch between windows. So, to go to the next window, just use the shortcut:
control + b and n
In turn, the return to the previous one is:
control + b and p
If we had a lot of windows open, the easiest way to jump to a specific window is:
control + b and numer
Nothing difficult isn't it? Have you noticed that windows 0 and 1 are called bash? This is because they take by default the names of the running program . When we run the top program on one of the windows, and on the other vim it will change the names.
If you want to rename a specific window, just perform key combinations on the active one:
control + b and ,
and then give our name for example we will change vim to editor and the effect will look like this:
Well, maybe at the end of the topic of windows one more thing: how to display a list of all windows? It is very simple.
control + b and :list-windows
Now let's move on to the session. We can start many sessions. Running tmux without arguments starts the default named 0. If you want the session to have a specific name, use the command:
tmux new -s session
session it is our name of the session.
The result will be:
Now the 2 most important functions of the session, i.e. the ability to disconnect from it and reconnect. We had started tmux, started a process on the window and we want it to run in the background. To do this, we disconnect from the session and the processes remain running. To make this happen, it is enough to use:
control + b and d
The terminal screen after starting our session called session and disconnecting from it will look something like this:
aciek@nielot:~$ tmux new -s session
[detached (from session session)]
To reconnect with last session we run command
tmux a
or if we want to connect to a specific session, e.g. our session called session, we execute the command:
tmux a -t session
the result will be connection to the session named session.
And now I would like to show the last thing for today, which is how to see the running sessions in the terminal window. This is very easy to do. It is enough to perform the sequence when we are connected in one of the running sessions
control + b and s
If there is more than one session, the terminal will look like the one below and you will be able to move the cursor, and at the bottom will be shown thumbnails of windows in a given session, and after acceptance we will connect to the session.
Well, we have knowledge about sessions, windows and panes in Tmux and we know how to move on them. We know how to start processes, disconnect from the session, and then reconnect. It's a great tool and I guarantee that it still has a lot to discover, and what we know is only the basics and these already give us a lot of opportunities. Have fun discovering more.
Ave :)