tmux - A Terminal Multiplexer
Tags: vim, tmux, terminal
Published: 2018-05-01 10:20:33

---
title: tmux - A Terminal Multiplexer
published: true
description: spawn several terminal sessions in a single window
tags: vim, tmux, terminal
cover_image: http://pmihaylov.com/wp-content/uploads/2018/05/Selection_023.png
canonical_url: https://pmihaylov.com/tmux-terminal-multiplexer/
---

Last time, I wrote about why I think everyone should try using a bare bones text editor like vim <a href="http://pmihaylov.com/vim-better-developer/">at least for a while</a>.

After such an experience, some might give up on it, while others might want to switch to vim full-time.

For the latter - congratulations!

But inevitably, a problem will occur with this approach, especially if you are working on a big project - <strong>using bare bones vim for big projects is quite unproductive compared to IDEs</strong>.

I used to use vim on my hobby side projects and it did a great job, but once I tried using it on a project with a large code base and several different build variants, things started to get messy and I preferred using the IDE.

Some of you might disagree with me, I have seen people prefer sticking solely to bare bones vim.

But in my experience, apart from the text editing boost it gives you due to its awesome user interface, all the auxiliary tools you need to maintain a big project are invaluable and not easily available from a bare bones vim.

So, determined to amend that, I started exploring many different tools and plugins to enable my vim to challenge the productivity my IDE provides.

In this new series of articles, I will share my favorite tools which help me keep myself productive everyday by using vim.

In the first one, the topic is my all-time favorite vim add-on - tmux.
<p style="font-size: 14px;"><em>
This article is part of the sequence <a href="http://pmihaylov.com/category/boost-your-vim/"> Boost Your VIM</a> where I share my favorite vim plugins and tools which can greatly optimize your productivity and make you a better keystroke ninja.</em></p>
<!--more-->
<h1 style="text-align: center;">So what is this tmux fuss about?</h1>
tmux is a terminal application, that allows you to emulate several terminals in a single terminal window.

That's all there is to it really. And it is more than enough.

Why is this necessary?

Well, have you ever had to do some editing in vim and perform some external terminal commands?

For example, do some changes and use git to add them to your repo and commit?

Or whilst doing that, you want to check something in a different directory, but you still want to preserve your current session?

Well, in normal circumstances, you would open another terminal window, do your work and return to the previous one.

But if you have to open too many sessions, things can, unfortunately, get hairy.

The problem is that opening a new terminal window means opening a new GUI application, which you can go to using the mouse easily, but navigating to it using keyboard stokes is harder.

And apart from that, if you want to observe the progress on several terminal windows at once, you would have to adjust their position on the screen using the mouse.

I don't know about you but whenever I have to move my hands from the keyboard to get the mouse, it feels bad.

And more importantly, you lose valuable keyboard frenzy inertia. So overall, your productivity drops.

<img class="alignnone size-large wp-image-693" src="http://pmihaylov.com/wp-content/uploads/2018/05/Selection_017-1024x547.png" alt="" width="676" height="361" />

In order to spawn all those terminals and order them in this manner it took me a looot of mouse clicks and movements.

If you have to do that often, it can quickly get quite frustrating, especially if you are deeply focused on your problem at hand.

But by using tmux, on the other hand, you can do all that work in a single terminal window with <strong>several keyboard strokes rather than by using the mouse</strong>.

<img class="alignnone size-large wp-image-694" src="http://pmihaylov.com/wp-content/uploads/2018/05/Selection_018-1024x576.png" alt="" width="676" height="380" />

In order to adjust the above layout, not a single mouse click was used.
<h1 style="text-align: center;">Install and Configuration</h1>
tmux is a separate software from vim. So you can install it the way you would install any other normal software.

On Debian distros:

<code>sudo apt-get install tmux</code>

On Red Hat systems:

<code>yum install tmux</code>

That's it! You have tmux on your system. You can run it by simply typing <code>tmux</code>.

However, the default configuration you get with tmux is pretty rugged and it can get you a bit irritated once you start using it for the first time.

For example, I recall there were some problems with copying text from a tmux terminal, there were issues with some colors and there was incompatibility when using certain shortcuts in vim with tmux.

In order to fix that, you will have to browse a lot to make tmux actually usable and user-friendly. If you like doing such stuff, be my guest.

But if you just want to get a normal, working configuration of tmux and start using it, you can copy my <code>.tmux.conf</code> file from my <a href="https://github.com/PreslavMihaylov/dotfiles/blob/master/tmux.conf">github repo</a>.

Simply copy the contents of the file I shared and paste it in a <code>.tmux.conf</code> file located in the root of your home folder. That is, the <code>~</code> directory.

That's that! Now you have a working tmux fully compatible with vim and (almost) not a pain in the ass to use.
<h2 style="text-align: center;">tmux is great! Can't I make it start automatically when I open a terminal window?</h2>
(flashes, buzzz, peew peew sounds. The Supervim guy lands in front of you)

Yes you can!

All you have to do is append these lines at the end of your <code>.bashrc</code> file (or whatever bash profile file you use) in your user home directory.

<script src="https://gist.github.com/PreslavMihaylov/bde8c1f04a4a5ee0a8828ae744d49cb9.js"></script>

<strong>But be wary!</strong>

If you add these lines and you break something in tmux's configuration, <strong>you won't be able to start a terminal at all</strong>.

So, if you are tweaking your <code>.tmux.conf</code> file, make sure you comment those lines out beforehand.
<h1 style="text-align: center;">But how do I use it?</h1>
Finally, the moment we've been waiting for. You got your new toy, you got it assembled. Now it's time to play with it!

So, you can use tmux just like you would use a normal terminal. There should be no other strange behavior observed.

With a single exception (I have found thus far).

Whenever you want to copy something from the terminal with the mouse, you need to <strong>hold shift as well as drag the mouse</strong>.

That's for the side effects of using tmux. Of course, if you skip the configuration part, you would have A LOT of side effects. But assuming you did that part well, there is nothing to fear.

Ok, now for the interesting part, how do you utilize the tool to spawn countless terminals inside your terminal and control them.
<h2 style="text-align: center;">The special tmux controller state</h2>
When you use tmux everything works as normal UNTIL you click <code>Ctrl-B</code>. This is a special key-binding which activates the <strong>tmux controller state. </strong>I have no idea how it is actually called, that's how I prefer to call it. I better patent it soon, hehe.

However you prefer calling it, it is the state in which you can use different keys and they will not act as they would in normal circumstances, but do something specific to control the multiplexed terminals.

If you don't do anything in this state for some time OR if you click the enter key, you will go back to the normal terminal state.

There are numerous keys you can use in this state to do all sorts of things. I will tell you only the most important ones, which are the only ones I usually use. For some other fancy use case, refer to the tmux man pages.
<h2 style="text-align: center;">Spawning a new terminal</h2>
<code>&lt;Ctrl-B&gt;"</code> - Spawns a new terminal vertically.

<img class="alignnone size-large wp-image-696" src="http://pmihaylov.com/wp-content/uploads/2018/05/Selection_019-1024x580.png" alt="" width="676" height="383" />

<code>&lt;Ctrl-B&gt;%</code> - Spawns a new terminal horizontally

<img class="alignnone size-large wp-image-697" src="http://pmihaylov.com/wp-content/uploads/2018/05/Selection_020-1024x577.png" alt="" width="676" height="381" />

These operations can be recursively used on inner tmux windows as well to create a beautiful grid of terminals.

<img class="alignnone size-large wp-image-698" src="http://pmihaylov.com/wp-content/uploads/2018/05/Selection_021-1024x577.png" alt="" width="676" height="381" />

Just a note on these two operations. After you use them, you go back to the normal terminal operation.

This means you can't chain terminal spawns like:

<code>&lt;Ctrl-B&gt;""""</code>

Instead, you have to Invoke <code>&lt;Ctrl-B&gt;"</code> separately several times.
<h2 style="text-align: center;">Navigating the grid of terminals</h2>
<code>&lt;Ctrl-B&gt;Left</code> - go to the left terminal screen.

<code>&lt;Ctrl-B&gt;Right</code> - go to the right terminal screen.

<code>&lt;Ctrl-B&gt;Up</code> - go to the upper terminal screen.

<code>&lt;Ctrl-B&gt;Down</code> - go to the lower terminal screen.

In contrast to the previous commands, when you invoke any of these ones, you will be left in the controller state.

This means you can combo these keystrokes to quickly navigate between screens.

<code>&lt;Ctrl-B&gt;Left Down Right</code>

And finally, when you reach the screen you want, click Enter to go back to normal mode.
<h2 style="text-align: center;">Resizing a cell</h2>
If you feel a terminal cell is smaller than you want it to be, you can use the following key bindings to adjust it's width and height:

<code>&lt;Ctrl-B&gt;&lt;Ctrl&gt;Left</code> -  increase width to the left

<code>&lt;Ctrl-B&gt;&lt;Ctrl&gt;Right</code> -  increase width to the right

<code>&lt;Ctrl-B&gt;&lt;Ctrl&gt;Up</code> -  increase height towards the above cell

<code>&lt;Ctrl-B&gt;&lt;Ctrl&gt;Down</code> -  increase height towards the cell below

It's easier if you try for yourself to get the hang of it.

Again, similarly to the previous operations, these leave you in the controller state.
<h1 style="text-align: center;"> Conclusion</h1>
That's all you need to know in order to get started with tmux. This is enough knowledge to get you to level ground to actually use it efficiently.

The rest is practice and getting used to the new toy.

Hope you enjoyed it!

But that is nowhere near enough to boost your vim and get it to level ground with a modern IDE in terms of tooling and enhancements.

What about navigating your project structure? Finding usages of functions? Auto-completion?

We are just getting started!

So stay tuned for more awesome tools that can turn you into an efficient vim keystroke ninja.