TMUX Terminal Multiplexer Tutorial

 

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:

  • A clearly defined cilent/server model (windows are their own clients which allows flexibility on how you handle windows. You can attach and detach different windows in different sessions without any issues)
  • Consistent, well-documented command interface. (You can use the same commands interactively as in the .tmux.conf file, more on that later)
  • Easily scriptable
  • Multiple paste buffers
  • Vi & Emacs keybindings
  • A more usable status line syntax (which also allows you to embed the output of a shell command, handy indeed.

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* You can change keybinding Ctrl-b to Ctrl-a by creating a ~/.tmux.confand putting the following into it: set -g prefix Ctrl-a .There is no tmux.conf by default.

  • Ctrl-b c Create new window
  • Ctrl-b d Detach current client
  • Ctrl-b l Move to previously selected window
  • Ctrl-b n Move to the next window
  • Ctrl-b p Move to the previous window
  • Ctrl-b & Kill the current window
  • Ctrl-b , Rename the current window
  • Ctrl-b % Split the current window into two panes
  • Ctrl-b q Show pane numbers (used to switch between panes)
  • Ctrl-b o Switch to the next pane
  • Ctrl-b ? List all keybindings

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).

1

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:

  • Ctrl-b n (Move to the next window)
  • Ctrl-b p (Move to the previous window)
  • Ctrl-b l (Move to the previously selected window)
  • Ctrl-b w (List all windows / window numbers)
  • Ctrl-b <window number> (Move to the specified window number, the default bindings are from 0 – 9)

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).

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:

2

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

  • Ctrl-b % (Split the window vertically)
  • Ctrl-b : “split-window” (Split window horizontally)
  • Ctrl-b o (Goto next pane)
  • Ctrl-b q (Show pane numbers, when the numbers show up type the key to goto that pane)
  • Ctrl-b { (Move the current pane left)
  • Ctrl-b } (Move the current pane right)

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 <space> 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:

3

4

5

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.

Original Article:

http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/

This entry was posted in Linux, Computers, etc and tagged , , , . Bookmark the permalink.

19 Responses to TMUX Terminal Multiplexer Tutorial

  1. Eric Lake says:

    Nice overview of tmux. I have started using it in place of screen and this tutorial helped with that migration. I am looking forward to Part 2.

  2. Jeff says:

    Glad you found it helpful Eric…..

  3. OK, now how do you create a bunch of windows/panes from a tmux configuration file? If I stick the commands into a file and do “tmux -f whatever.tmux”, nothing happens. But then when I quit tmux and run “tmux attach”, I see my windows/panes. What am I doing wrong?

  4. Lalo says:

    Thank you for the intro, I think you can split windows (horizontaly) by doing C-b and then ” instead of C-b: split-window, I like the | and – alternatives better. Now I’ll read the man page :).

  5. Sébastien says:

    I’m using tmux without having used screen before, always thought it might be too tough for me. I’m very pleased with tmux, using ssh a lot it helps very much.

    Could you tell me what are the two top panes running, on your first screenshot?

  6. Jeff says:

    OK sure,

    The programs are htop, iftop, and iotop in that order top to bottom.

    Do a search for “Linux terminal programs” sometime when you have some time to kill.
    My first such search resulted in more available than I would have ever thought!

  7. Robert Daeley says:

    (Edit by Jeff): Spam filter didn’t like your original comment with ping back.

    A Tmux review by Robert Daeley at http://www.celsius1414.com/2011/07/04/learning-tmux/

  8. Marcel Dorenbos says:

    I use tmux for a while now to connect to multiple systems at the same time. I automatically rename the windows using the following (bash) logic:

    tmux rename-window “Start $host”
    ssh $host
    tmux rename-window “End $host”

    So in the tab name I can see which host I connect to, and in which state it is. Well, that is actually how I thought it would work, but tmux will not rename the session from which the command was given, instead it is renaming the window which is active.

    I have been looking in the man pages and on the web, but it seems to be impossible to rename “this window” instead of the active window.

    Do you have any suggestions here?

    Thanks!

  9. Marcel Dorenbos says:

    I am now using a sort of workaround, which is to rename the window to something unique, find out using the /tmux list-windows/ command what the window number is, and than rename the windows using this number.

  10. RP says:

    I like tmux, it’s strong and easygoing. The problem is that I could alias “irssi” to “screen irssi” and jump directly into screen :) I can’t do that with tmux, unfortunately. That doesn’t stop me from using it, though. If there’s any suggestion of how I can start an app with tmux directly, it’s welcome.

  11. rking says:

    Hi! Sweet tutorial; I’m thinking about linking to it from tweemux.

    I’d like to talk about a few details, but before I get into it I figure I’ll test the waters and see if you’re interested in some feedback.

    The first thing I’d ask if you’d be OK with removing the “unbind %”. Yes, it’s an awkward mapping, but my opinion is that the default keys should usually be left alone, because if you’re pair programming with someone that isn’t used to your config, it can be surprising.

    What do you think?

    Thanks!

  12. Shane Hudson says:

    I’ve been meaning to use tmux for years and have now finally set it up, thanks for your help.

  13. Luca Profico says:

    Thanks a lot. After 4 years, I still come back to this page when I forget a shortcut :)

  14. Jeff says:

    Glad to hear it’s been helpful to you Luca, and thanks for the comment.

  15. Dave says:

    Dude…this post now has a higher google search ranking than your “original source” — the only problem I see with that is that this is plagiarism (unless you wrote the original?).

    You haven’t changed or added *anything* to the original source, simply rewritten some of the phrasing. Everything is the same, right down to the .tmux.conf customization and where the post ends.

    You actually just *pasted in* the final paragraph from the original. This is blatant plagiarism.

    That sucks, man :-(.

  16. Jeff says:

    OK, you must be Cody, who posted the instructions for tmux? Did you copy and paste anything from the man pages that weren’t available in the package I used when I installed tmux?

    Whats that …. Oh you mean you’re not Cody! Well in that case you can go fuck yourself!

    Would you accuse me of plagiarism if I were to copy and paste a bit from a manpage on tmux? Hmmmm …. honestly, in GPL or BSD licensed software I cannot wrap my head around getting accused of plagiarizing by posting the use instructions.

    At the time I installed tmux, it had no man pages with the package I used. I searched for some time to find that source. I never claimed I developed the software or wrote the documentation on it…. I simply found a source of info and wanted to share it with others who found themselves in a similar situation. After I tried it out, see the screen shots, I put the missing instructions up on my blog with a title, etc that made finding it easier for the users.

    I offer info and my opinion on this blog at no cost to the readers, to share with others in the spirit of open source software. I have no advertisement funds or any other means to financially support this blog other than my volunteering of time and funds.

    If you’re not Cody, a tmux dev, and have a problem with anything on my blog, don’t read it. If you’re an Arch Linux cheerleader who doesn’t like what you see here, by all means please feel free to read all you like, just don’t post! Perhaps you could use a different perspective on some things.

    Dave, you should find any similarities between the tmux instructions and the current man page on tmux, then go …. you probably guessed it this time so I won’t even say it.

    If you’re Cody, speaking for Cody, or you’re a tmux developer, please accept my apologies if you consider this plagiarism, none of my rant was directed towards you, and I’ll take it down. An email would be more appropriate for communicating about this so please email me.

    Thanks for your input, Jeff

  17. Dave says:

    Jeff,

    I ……………….. Sorry, I didn’t mean to offend you with my first post,……………..

    [ JEFF’S *AUTOMATIC BULLSHIT DETECTOR, EDITOR, AND ELIMINATOR* KICKS IN ON THIS REPLY FROM DAVE ]

    QUOTE:
    ” the original blog post — is under copyright *by default* “

    Dave,
    LOL … Enough said right there, I’m keeping it for the humor aspect and deleted the rest. We are done discussing this. Please don’t bother with such off topic comments.

    Thanks, Jeff

  18. Jeff says:

    email sent to dave

Comments are closed.