TMUX - The Terminal Multiplexer (Part 1)

Posted By: cody

Last Updated: Monday June 28, 2010

tmux is similar to screen as it lets you run numerous TTY’s in the same terminal window. It supports some very cool and intuitive features natively as well as a much more readable configuration syntax (ever looked at a .screenrc file?).

Why TMUX over Screen?

Well according to the FAQ for tmux it has the following advantages over screen:

Default keybindings & Functionality

The default keybindings for tmux are actually pretty intuitive, though if you’re used to screen you’ll likely get a little peeved with the default action binding of C-b, though this is easily changed to mimic screens behavior:

*NOTE* If you’re like me the Ctrl-b binding isn’t horribly intuitive especially if you’re used to screen. You can rebind this by putting the following in ~/.tmux.conf:

set -g prefix Ctrl-a

Now these are pretty self explanatory - the real magic (for me) of tmux is the ease of modifying the default behavior to do what you want, but first things first: let’s explore the default behavior of tmux.

Basic Window Handling

Start up tmux with the tmux command and you should be greeted with a simplistic terminal window that resembles screen - the only difference is it has a default status bar which is nice (easily added to screen as well).

Terminal Multiplexer

Now let’s create a couple of windows and go through them (we’ll be using the default bindings). Hit Ctrl-b c a few times to create a few windows, you should notice that there are more tabs in the status bar.

Now if you’re like me you like to have descriptive names of which each window is for, so let’s rename them by hitting Ctrl-b ,. It should prompt you to rename the current window - type anything you want and hit enter. Now the current window is renamed to what you specified. Now going forward I’m going to have two windows open respectively named “window1” and “window2”.

Once you rename your windows lets switch back and forth. We have several different ways of switching windows, so I’ll go over the ones I personally use:

Now these ones fairly self explanatory however they don’t really cater to a lot of different windows. What if you have 10+ windows open? It becomes quite tedious to find the window you want - but don’t fret! Tmux has a find-window option & keybinding. Type Ctrl-b f and type in the window name you want (it actually searches for the window so you can type in only part of the name of the window you’re looking for).

You can also get a list of the windows in the current session by executing the list-windows command. To execute commands interactively you type Ctrl-b : which will bring up a text prompt. From there you can execute any command tmux supports interactively (tab completion is supported).

Terminal Multiplexer - Interactive PromptTerminal Multiplexer - List Windows

Basic Pane Handling

One of the most powerful features tmux offers is the ability to split up your current window into “panes”. Anyone whose familiar with tiling windows managers will feel quite at home. It’s a bit difficult to explain this in words so a simple screenshot will suffice:

Terminal Multiplexer - Split Windows

Now here are some basic key bindings and commands to split the terminal window (vertically and horizontally) and to switch between them

Now some obviously the default bindings don’t encompass some of features, such as splitting horizontally. I personally rebind the keys so “|” splits the current window vertically, and “-” splits it horizontally. Not the easiest things to type, though easy to remember.

You can achieve this by putting the following in ~/.tmux.conf or by typing it in the interactive prompt (Ctrl-b :). Keep in mind if you do the latter it will only be in effect for that session:

unbind % bind | split-window -h bind - split-window -v

Advanced Window Handling

Now that we went over the basics lets dive a little deeper into some “advanced” features of tmux. This includes moving windows around, linking windows together, switching windows from different sessions and much more. By default tmux doesn’t have key bindings for these features, so we’ll be entering them in the interactive dialog (accessed by typing Ctrl-b :) - keep in mind tmux is very scriptable and you can easily create your own key bindings for all of these.

Moving Windows

Now if you want to move a window you can use the move-window command. The command to do this:

move-window [ −d] [ −s src-window] [ −t dst-window]

swap-window [ -d] [ -s src-window] [ -t dst-window]

Similar to the above command except both windows have to exist - if they both do the window with the ID source and destination windows will be swapped.

Advanced Pane Handling

When you split up a window into multiple smaller windows they’re referred to as panes. Tmux also offers “layouts” for the panes, or the default positioning and behavior when you create a new window. You can switch through the panes by using the key binding Ctrl-b which will toggle through the different layouts. Each one has different behaviors such as main-vertical which means the current active pane will take up more space in the current window, or even-vertical which will split the panes equally. Since this is difficult to describe in text I believe a few screen shots are in order:

Now that you’ve seen the different layouts let’s see what we can do with these panes. As mentioned above in the “Pane Handling” section you can switch through panes by issuing the Ctrl-b o key combination (which is using the down-pane command) or by typing Ctrl-b q which will list the pane ID’s and you select the one you want.

Make your pane into its own window

If you want to take a pane and make it into its own window you do the following:

Ctrl-b : “break-pane”

Simple enough, you should now have the pane in its brand new window. If you don’t want it to automatically make the pane you just broke out as the active window issue the “-d” switch which will simply break the pane to a new window but keep you in the current window.

Resizing Panes

You can also resize panes if you don’t like the layout defaults. I personally rarely need to do this, though it’s handy to know how. Here is the basic syntax to resize panes:

Ctrl-b : resize-pane (By default it resizes the current pane down) Ctrl-b : resize-pane -U (Resizes the current pane upward) Ctrl-b : resize-pane -L (Resizes the current pane left) Ctrl-b : resize-pane -R (Resizes the current pane right) Ctrl-b : resize-pane 20 (Resizes the current pane down by 20 cells) Ctrl-b : resize-pane -U 20 (Resizes the current pane upward by 20 cells) Ctrl-b : resize-pane -L 20 (Resizes the current pane left by 20 cells) Ctrl-b : resize-pane -R 20 (Resizes the current pane right by 20 cells) Ctrl-b : resize-pane -t 2 20 (Resizes the pane with the id of 2 down by 20 cells) Ctrl-b : resize-pane -t -L 20 (Resizes the pane with the id of 2 left by 20 cells) … etc

Hopefully you get the jist - don’t get confused! Simply load up a tmux session and split the window a couple of times and issue the above commands. It should become fairly evident how it behaves after fiddling with it for a bit.

Utilizing the client / server model

I’ve avoided mentioning that a lot of these commands can actually be applied to numerous tmux sessions which allows quite a bit of flexibility - the reason for avoiding it is it’s too much information all at once! An example of using this functionality is if you have two sessions open you can “link” or “move” windows across different sessions - unfortunately the actual “how-to” will be in Part 2.

Conclusion

Tmux may be a bit confusing however it’s worth putting in a few minutes to check it out and see what it has to offer - quick easy and intuitive.

Upcoming in Part 2

I’ll be going over several tips and tricks for tmux including:

Ready to get started? Build your site from
$2.24/mo
GET STARTED NOW