Why tmux as primary Terminal

Why tmux as primary Terminal

I love working with the command line. Seriously, I think there’s hardly any more productive and more versatile tool for a software developer than the terminal. Maybe it’s the hacker/wizard/neckbeard kind of feeling I get when using a terminal, I don’t know.

At work, we do code sometimes :D. Everyone’s got their own laptop and can set it up the way they like. And since I love working with the command line I spent quite some time doing (only sane! I swear!) modifications to my terminal environment that make working with the command line more pleasant and streamlined. This is why my colleagues usually will be greeted by something like this:

my tmux style

If they’ve worked with me before they know what they are up to. But every once in a while there will be a new team member who doesn’t know my environment. Usually this is the point where they will ask something like “WTF am I looking at?” and it’s my time to shine!

To get start with tmux which is terminal multiplexer and sounds like Linux at the same time ;) there is no complicated things to do.First of all we need to install it which in my case we will using apt which is Ubuntu's(Debian based Distros) package manager.

sudo apt-get install tmux

To starting your first session you need to execute it

This will create a new tmux session with a green status bar at the bottom.

The status bar is an important part of tmux. Apart from the currently opened windows (on the left) it also shows some system information like date and time (on the right). The status bar can also be customized and I’ve seen some really fancy stuff around (upcoming calendar events, battery status, to name a few) but this is something we’ll leave for later.

All commands in tmux are triggered by a prefix key followed by a command key (quite similar to emacs). By default, tmux uses C-b as prefix key. This notation might read a little weird if you’re not used to it. In this emacs notation C- means “press and hold the Ctrl key"3. Thus C-b simply means press the Ctrl and b keys at the same time.

he 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. Woot Woot! You’ve just invoked your first tmux command and split your pane in two.

Where there’s a split into left and right, there’s also a split into top and bottom pane. To split a pane into top and bottom panes use the C-b " shortcut.

Right now we’re trapped in the newly created pane. But we really really want to go back to the left one. Switching to a different pane uses the C-b <arrow key> shortcut, where <arrow key> is the arrow key pointing to the pane you want to switch to. In our case we want to switch to the panel on the left so it’s C-b left for us. Just once more, so that we fully understand this: This means you press Ctrl and b (your prefix) followed by the left arrow key to get to the pane on the left.

You can now go ahead and split each of your new panels even further. Feel free to experiment and split your panes like a maniac to get a feeling for it.

How to create new Windows?

Windows in tmux can be compared to creating new virtual desktops; if you’ve ever worked with one of the major Linux deskop environments (KDE, Gnome) you’ll hopefully find this analogy helpful.

Creating new windows is as easy as typing C-b c (one last time: that’s Ctrl and b at once, then c). The new window will then be presented to you in tmux’s status bar.

You can now divide the pane in your new window as you like. Or don’t. That’s up to you.

To switch to the previous window (according to the order in your status bar) use C-b p, to switch to the next window use C-b n. If you’ve created many windows you might find it useful to go to a window directly by typing its number (the status bar will tell you which window has which number), just use C-b <number> where <number> is the number in front of the window’s name in your status bar.

Now you can control you sessions

tmux ls

This will give you a list of all running sessions, which in our example should be something like

0: 2 windows (created Sat Sep 1 15:51:34 2018) [199x44] (detached)

To connect to that session you start tmux again but this time tell it which session to attach to

tmux attach -t 0

If you prefer to give your sessions a more meaningful name (instead of a numerical one starting with 0) you can create your next session using

tmux new -s mysql

This will create a new session with the name “mysql”.
You can also rename your existing session

tmux rename-session -t 0 database

The next time you attach to that session you simply use tmux attach -t database. If you’re using multiple sessions at once this can become an essential feature.

And that’s it! Congratulations, you’ve just completed your first tmux session and got your hands dirty with its window and session management. Yes, there’s more stuff tmux can do. But what you’ve just learned should be everything to start using tmux in the future.

You can also take a loot at tmux page on github.