DEV Community

Cover image for TMUX - multitasking with your command line 🐧
Adheeban Manoharan
Adheeban Manoharan

Posted on • Updated on

TMUX - multitasking with your command line 🐧

I had to code a job for a project that I was working on that does the following:

  • Fetches the data from the source database
  • Fetches the data from the destination database
  • Compares the data between source and destination based on a common unique identifier
  • Returns the keys that are in source and not in destination.

Yes, basically this script is for retrieving the missed out data.

Now the job was running fine and it gave out the desired results, the source database had around 200M+ data points and the destination had around 180M+. So, naturally this job ran for like at least 7-8 hours on a medium performance remote Linux server and the server handled it well without breaking a sweat.

The problem is the duration for which the job ran. I would trigger the job the previous night and after a good night's sleep, would wake up only to find that the terminal session closed before the job could complete its run. This was frustrating, as every time I had to reset some parameters wipe out the half baked data that got inserted into the destination database before running the job again and all this required a lot of approvals.

I was searching for some sort of a session holder, and this is when tmux came to the rescue πŸ¦Έβ€β™‚β€. When I did some research tmux was already pretty popular with the linux geeks.
tmux multiplies your productivity by multi-fold and people swear by it.

What's tmux?

According to the author's of the tmux

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached

Yes, it exactly does what they say. It can split a same terminal session into multiple small panes or windows and each of tmux sessions will be kept alive in the background until the tmux server is killed.

Now, the MacOS users would argue that we already have iTerm2 for that. A much more convenient GUI tool. Yes, I love iTerm2 and I have been using it ever since I discovered that amazing tool. iTerm2 combined with ohmyzsh is like CLI on steroids. This beautiful combo deserves a blog of it's own and let me know if you want me write about it πŸ˜‰

Now, to answer our MacOS guy, iTerm2 is great for GUI OS's like MacOS and windows. Our best bet for CLI OS like Linux is tmux and there's no doubt at it. Often times you'd find yourself working on lots of remote servers as a developer, where there would not be a lot to be seen except for the CLI and this is where tmux comes in handy. Okay, enough of the intro let's dive in.

Getting started with tmux

To install tmux, just do


 install tmux

``` πŸ‘† for MacOS **OR**
```sudo

 apt-get install tmux

``` πŸ‘† for ubuntu and it's derivatives. This should get the job done.

Now that we have installed tmux on our machines, let's start our first tmux session shall we? To start a tmux session simply type `tmux` into your terminal or if you fancy a more sophisticated command do
```tmux

 new -s <session_name>

Enter fullscreen mode Exit fullscreen mode

Here the session name is the identifier of the session you start. if you don't pass a session tmux will take 0 as the ID for it's for it's first ever session in the machine and increments it as the no of tmux sessions increase. I have created a session with the name tmuxrocks, because why not?
After creating a session doing a tmux ls will show us the number of active tmux sessions in the machine.

tmux ls

The green bar at the bottom tells you the session ID and the shell that you are working on. Voila! we've created our first tmux session.

Splitting panes

Now, let's take a look at the wizardry of tmux, shall we? Let's split the session into some panes.

All commands in tmux are triggered by a prefix key followed by a command key. By default, tmux uses C-b as prefix key. Here, the notation C- means "press and hold the Ctrl key". Thus C-b simply means press the Ctrl and b keys at the same time.

The shortcut to split panes into a left and a right pane is C-b %. Remembering what I’ve just told you about tmux’s sequence of prefix and command key this means to split your single pane into a left and a right pane you press Ctrl and b at the same time, release both, and then type the % key. This will create a second right pane.

To further split the pane into horizontal type the " key after pressing C-b and for navigating between panes, you simply have to press C-b and then press any of the arrow keys, depending on the pane you want to switch to (i.e If you want to switch to the right pane, press C-b and then the ➑ key). To quit a panel simply type exit. That'll close the current pane you're in. There we go, we've learned how to split and navigate between panes in tmux.

tmux split panes

Just in case if you're wondering what are those alien looking things appearing in my CLI, that is NMS (no more secrets), just a tool to prank your friends. I'll leave a link as well😺

Alright coming back to tmux, We can always reattach to a session if we know the session ID of that session. Even months after we had created that session, it will be kept alive in the background, provided that the machine should not rebooted after creating the session.

to reattach to a session simply do


 attach -t <session_name>

Enter fullscreen mode Exit fullscreen mode

If you want to rename an existing session, you can do


 rename-session -t <current_session_name> <new_name>

Enter fullscreen mode Exit fullscreen mode

Congrats! you just got your hands dirty with your first ever tmux session. This should be enough to get you started with tmux. If you want to learn more about tmux I'll leave a link for a youtube playlist that I found useful for you to explore further. Alright, see you in the next post. Cheers!

Learn tmux - YouTube

Tmux is an absolutely fantastic terminal multiplexer, allowing you to become more efficient on the Linux command-line. Check out this short but sweet series,...

favicon youtube.com

Top comments (0)