1. 程式人生 > >tmux — Real Estate Agent for Your Computer

tmux — Real Estate Agent for Your Computer

tmux — Real Estate Agent for Your Computer

“black laptop computer turned on” by Lewis Ngugi on Unsplash

This one’s technical. Don’t read it unless you know what a computer terminal is. But if you do, this can change your life!

What Can It Do For You?

How many terminal tabs do you open every day? Or maybe you use separate terminal windows? Are you sure you’re making the best use of the available space?

Even though our screens are getting bigger and denser, terminal applications don’t make the best use of it. Having your 4K monitor displaying nothing more than git log seems like a waste of space. Moreover, if you use your terminal you rarely have just one application open at a given time. You may be writing code in vim

, compiling stuff, watching logs, downloading files with wget or testing an API with curl. Some of those applications are long-running, others are one-shot. But they all require a shell and some screen space.

If you ever wondered how to make more than one terminal application fit a screen elegantly,

tmux may be a solution for some of your problems.

Why I think tmux is better than having several terminal windows open? Simply:

  • you can have multiple terminal applications visible at once
  • you can arrange your applications however you like and they stay that way (unlike regular windows)
  • you can have a persistent session
  • you can reattach to an accidentally closed terminal window
  • you can run several remote applications over a single SSH session
  • you can leave your remote session running even after you disconnect from the machine

Why It’s Good?

Most of my time at the computer I spend in the terminal. I write my prose in the terminal, I write my code in the terminal, I work in the terminal. I even used to play in the terminal (check out Rogue or Nethack to see what I mean).

As a terminal dweller, I like to be able to make the best use of my environment. One tool that I adore and can’t imagine living without is tmux. Tmux stands for terminal multiplexer and it pretty much sums up what it does.

You see, normally, the terminal can run only one application at a time. You open up one and you do your work. To open up another one you need to either close or suspend the previous one. Back in the days when terminals were only 24 lines of 80 columns and computers couldn't handle much workload, this made sense. Nowadays, when 4k monitors are the norm, the available real estate can be used in a more efficient way.

With tmux, your precious terminal space does not go to waste. Tmux introduces a concept of windows and panes so your single terminal session can have multiple windows open (a window occupies the whole terminal space, think of it as a browser tab) each with one or more panes, where panes are the places that can run applications. You can split panes horizontally or vertically and resize them as you like.

This way one terminal window can show some long-running job in one pane, logs in the second and you still have a shell handy in another pane. There are some other cool features available as well, such as session management. It means you can close the terminal window and later reopen it with the same state you left it earlier. Neat, right?

Let me show you an example interaction.

tmux in action

In the above video (also available on asciinema) I did following:

  • started tmux
  • split the window horizontally with ctrl-a -
  • changes the active pane with ctrl-a k
  • split the window vertically with ctrl-a |
  • started RabbitMQ
  • changed the pane back to the original one
  • opened Vim
  • zoomed in the pane containing Vim with ctrl-a z(note the ^Z symbol in the status bar)
  • zoomed out again (ctrl-a z)
  • detached tmux
  • attached the same session again to show it’s intact

How I Use It?

Multiple panes

In a tmux world panes are, simply put, pieces of screen. You divide your large terminal workspace into smaller chunks by splitting the screen either horizontally or vertically. I use them to see different applications relevant to one task. It can be an editor with code, shell for execution, another one for tests, and a compilation output.

It looks like this:

Each pane has its number.

Multiple windows

Panes reside in windows. They can have distinct names, so I name them after a task, if I name them at all. This way I might have one window titled lbwhere I have panes opened for Load Balancer shell, logs and network statistics and another window titled backend where I see a shell with a web application together with logs and a top window.

Multiple Sessions

If panes are views related to a single task and windows are tasks, then I like to think of tmux’s sessions as “projects”. I usually have a session open for each project I am currently working on. I may have sessions called openhome, writing and topsecret. This would mean three ongoing projects: one for OpenHome development, second for writing and a third one that’s hush-hush!

Status bar

At the bottom of each tmux session, there is a status bar. By default, it shows the available windows, hostname, time and date. But as with most things in tmux, the status bar is also customizable. You can turn it off when you don’t need it and you can control what information to present there.

For example, you may not need to see the hostname, but may perhaps be interested in some other information. Like a battery status indicator or CPU and memory monitor. There are plugins that can show agenda from your calendar in the status bar. For me, this is the best place to place a Pomodoro timer (more on that in a moment). Like a shell prompt, the status bar can give you an instant glance at what is happening at any given time.

Plugins

Thanks to Tmux Plugin Manager, tmux allows you to use and write plugins. Why are plugins such a great idea? They make configuration file much more readable. Typically, a single plugin serves a single purpose and offers some customization. You can structure your configuration into three distinct sections:

  1. List of plugins
  2. Plugin options
  3. Everything else

Here is an excerpt from my ~/.tmux.conf:

Some plugins add missing functionality to tmux itself, while others are more like prefabricated configuration. tmux-sensible is a good example of the latter. It’s just a collection of settings that most people agree upon.

Notable plugins:

  • tmux-resurrect and tmux-continuum are two plugins that made my switch to tmux a no-brainer. The first one allows saving and restoring of a current state. All your sessions, windows, panes along with their contents are stored in a file. After a system restart you can get safely back to where you left from. tmux-continuum takes this one step further with automatic backup every few minutes and restoration of a saved states after you relaunch tmux. This way a sudden system restart is no longer a threat.
  • Tmux Pomodoro. Are you familiar with the Pomodoro Technique? With this plugin, you can have your own Pomodoro timer right in your status bar. The plugin integrates with system’s notifications, so you won’t miss a break if your terminal is not visible.
  • Tmux Man. With this plugin you can open documentation by pressing a key combination and entering the term that interests you. Documentation pops up in a separate pane that you can close when ready.

Want More?

Do you think it’s worth to try tmux yourself? Or maybe you want to take your relationship with tmux to the next level? Awesome tmux is a good place to check available resources for beginners and advanced users alike.