loading...
Cover image for Making your terminal more productive with Tmux

Making your terminal more productive with Tmux

srbruning profile image Diego bruning ・4 min read

original post: Deixando o seu terminal mais produtivo com o Tmux in diegobruning.com

Tmux is a terminal multiplexer. This means that you can start a Tmux session and open multiple windows within that session. Each window occupies the entire screen and can be divided into rectangular panels.

With Tmux we can easily switch between different programs in one terminal, disconnect them and connect them again to a different terminal.

Programs running on Tmux will continue to run even if we are disconnected, as Tmux sessions are persistent.

All commands in Tmux start with a prefix, which by default is

ctrl + b

.

Installing Tmux:

We can install Tmux in two ways: through the package manager of the operating system or through the official repository.

Installing Tmux from the official repository:

First check the latest version in the official repository and then change the version in the commands that follow (in my case the most recent version was 3.1b):

# downloading
$ wget https://github.com/tmux/tmux/releases/download/3.1b/tmux-3.1b.tar.gz
# unzipping 
$ tar -zxvf tmux-3.1b.tar.gz
# accessing the folder
$ cd  tmux-3.1b/
# Setting 
$ ./configure  && make 
# Installing 
$ sudo make install

When executing the command "./configure && make" on a machine with ubuntu I had the following errors that you may have:

configure: error: no acceptable C compiler found in $PATH
lconfigure: error: "ibevent-dev"
configure: error: "ncurses-dev"
para cada um uma solução:

$ apt-get install build-essential
$ apt-get install libevent-dev
$ apt-get install ncurses-dev

Installing Tmux via the package manager:

Ubuntu and Debian

$ sudo apt install tmux

CentOS and Fedora

$ sudo yum install tmux

macOS

$ brew install tmux

Session:

Now with the installation complete, just run the tmux command on the terminal, if everything went well you will see a green bar at the bottom of the terminal.

When running tmux a session is created and the programs running in this session will continue to run even if the terminal window is closed.

In this green bar we have some information:
In the right corner, from right to left we have the current date, the time, and the name of the current computer.
In the left corner between brackets we have the current session, then we have the window (in case 0) and the name of the software that is running (in the case bash). The '*' indicates which window is currently active.

Tmux has a "base" shortcut for executing the commands which is ctrl b, we will call this shortcut prefix, so whenever we quote the prefix key we will be talking about this combination.

To list the sessions use the command:

$ tmux ls

| 0: 1 windows (created Tue May 26 13:11:04 2020) (attached)
0 is the name of the session, 1 windows means you have a window open in the session.
With the session started we can use some shortcuts to execute commands in Tmx. For a list of all Tmux commands, type:

ctrl+b ?

Creating named Tmux sessions
By default, sessions in tmux are named numerically. Sometimes it can be useful to create a named session. We can do this with the command:

$ tmux new -s session_name

Disconnecting from a session
You can disconnect from the Tmux session and return to your normal shell by typing:

$ Ctrl+b d

If we run a program before disconnecting, it will continue to run even after we disconnect.

Reconnecting to a Tmux session:
First we need the name of the session, we can get this with the command "tmux ls". With the session name, just execute the command (replacing sessao_nome):

$ tmux attach-session -t sessao_nome

Windows

With tmux we can create windows from within the same session. To create a new window we use:

$ Ctrl+b c

We can see that in the green bar two windows will appear and one of them will be marked with the ‘*’ that indicates the current window.
To switch to the previous window we can change the command:

Ctrl+b p

para mudarmos para o próxima janela:

Crtl+b n

and to change to a specific window just use the prefix and the window number:

Ctrl+b 0

We can rename a chord window with its own criteria, for that we use prefix +, :

Ctrl+b ,

if we want to close a window we can use the prefix + &:

Crtl+b &

Panels

We can still divide the windows.
Tmux allows you to divide vertically with the prefix + %:

Ctrl+b %

And horizontally with prefix + “:

Ctrl+b “

To navigate between the panels we use prefixes and arrows.

We can convert a window panel with a + prefix!:

Ctrl+b !

To kill a panel we use the prefix + x:

Crtl+b x

We can also zoom in on a panel with the prefix + z, and then with the same command return to shared view:

Crtl+b z

Customizing Tmux

When Tmux is started, it reads its configuration parameters in ~/.tmux.conf.

We are not going to detail this configuration, you can get more details on the project wiki. But we can for example change the color of the bar with adding the following configuration in the file:

# Customize the status line
set -g status-fg  green
set -g status-bg  black

Conclusion

This article was a quick introduction, there's a lot more on the project wiki and in the user manual, feel free to check it out, but you can start using it as we saw.

Try it and then comment here if you felt any advantage in using it.

Posted on by:

srbruning profile

Diego bruning

@srbruning

More than 5 years of experience in software development. diegobruning.com

Discussion

markdown guide
 

I particularly like ctrl + f with one hand without leaving home row since I have ctrl swapped with caps. I use resurrect, continuum and prefix highlight plugins.