TMUX – The Terminal Multiplexer (Part 1)

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* 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

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

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:

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

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

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

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:

  • Custom hotkeys
  • Custom window styles & colors
  • Scripting with tmux (bash, sh)
  • How to use the server / client model further
This entry was posted in General, Tips and tagged , , , , . Bookmark the permalink.

84 Responses to TMUX – The Terminal Multiplexer (Part 1)

  1. codyr says:

    @Brandon – I don’t receive an error strangely enough! I’ll modify the post however to reflect this as I may have been using an older package when I initially wrote it.

  2. Pingback: Booting up tmux sessions with customized windows and panes « Notes about coding

  3. Pingback: tmux : un remplaçant à screen ? | Un blog de geek 2.0

  4. Pingback: Development: the ninja way : Andrei Lisnic blog

  5. Philip Jagielski says:

    Can you please link to your part 2 at the bottom? I’ve visited this page several times and never realized you got around to a part 2 until I found it in an unrelated google knew :-)

  6. Luce says:

    In the Default keybindings section above you might want to correct:

    set -g prefix Ctrl-a

    to

    set -g prefix C-a

  7. Pingback: bschaefe.net » Taking TMUX to the next level

  8. Vinicius Assef says:

    Actually, it woud be set-option -g prefix C-a, in default keybindings section.

  9. Fred says:

    man page says:

    Changing the default prefix key:

    set-option -g prefix C-a
    unbind-key C-b
    bind-key C-a send-prefix

  10. Pingback: Tmux | MY KNOWLEDGE LIBRARY

  11. Arun says:

    Very nice information. Thanks. I am new to Tmux and your post has provided me with good start.

  12. ZIADI says:

    Thanks a lot :)

  13. flo says:

    thank you for this post. i literally just threw away screen forever.

  14. I, like the previous commenter thank you for writing this up. As of today, screen is no more!

  15. plariv says:

    A friend suggested I take a look at tmux, which I’d heard about, but never tried. Shoulda done it much sooner… I’m sold on it! Found your post while as I was looking for startup guidance. Thanks for the tips… direct, succinct and most helpful.

  16. Pingback: Using tmux to remotely work on Raspberry Pi | Saju's Tech Blog

  17. mesuutt says:

    Good tutorial bro. thanks :)

  18. Max Barraclough says:

    Thanks for the useful article.

    A useful shortcut I found on Dayid Alan’s cheat-sheet that you don’t mention here is Ctrl-b “ to split into left and right panes, similar to how Ctrl-b % splits into top and bottom panes; there’s no need to use Ctrl-b : split-window.

  19. omartrinidad says:

    Good tutorial, I am starting with tmux, it has been a valuable resource.

  20. Pingback: Tmux scripting | Htbaa blogs?

  21. Pingback: Anyone familiar with splitvt?

  22. Pingback: iTerm2 + tmux + vim

  23. Hannes Steffenhagen says:

    “The default keybindings for tmux are actually pretty intuitive”

    Nonsense. There is no such thing as intuitive keybindings. If anything, they may be familiar – that is, similar to something else you’ve used. But intuitive – that is, accessible without prior knowledge – no way.

  24. anon says:

    Hey, thanks a lot for this tutorial. I uninstalled tmux the first time due to the sheer lack of easy starting info, and the daunting man page. Now, thanks to you, I’ve gotten some awesome split planes and couldn’t love tmux more :)

  25. Pingback: TMux, a great tool! | Quarbby

  26. Pingback: Tmux – Terminal Multiplexer | ebeab

  27. Shahid says:

    The tutorial is too nice ! i was stuck at horizontal pane and solved it just !
    thanks a lot

  28. Agge says:

    You might want to add that Ctrl-b c is supposed to mean press ctrl and b then c.
    That part is not intuitive if you havent been in a program where you use that sort of keys.

  29. Arun singh says:

    Previously i was using Screen for this task, but knowing features of tmux i want to give a start and your article helped me to start with tmux. Screen and tmux both are nice but i think screen is light weight and easier to use.

  30. Gareth says:

    Thanks for the post, very useful to a newbie!

    Little question…. How I have opened the time (Ctrl-b t) – how do I exit this and get back to the window?

    I also end up stuck if I open shortcuts (Ctrl-b ?), but I’m guessing the resolution to exiting the time will work for exiting the shortcuts.

    Cheers

  31. Warbo says:

    One difference I’ve noticed from screen is that tmux’s panes are contained inside a window, whereas in screen it’s (roughly) the other way around. Switching window in screen (ctrl-a “) will only change the contents of the current ‘pane’, whereas in tmux switching window (ctrl-b w) will swap from one set of panes to a different set.

  32. Proinsias says:

    Nice article, thanks.

    Just a quick note to add regarding the resizing of panes. If you hold down Ctrl+B & tap an arrow key you can easily resize the panes on the fly.

  33. Dave says:

    I know this is an old post, but I just wanted to know that it’s been eclipsed in the Google search rankings by a plagiarized version, found here: http://www.jeffstory.org/wordpress/?p=132

    This guy copied the entire post, changed some of the wording, made absolutely no meaningful modifications, and now ranks first in the search results for “tmux tutorial”. I mean, he copied *everything*, including the content, .tmux.conf customization, and even the entire last paragraph (verbatim).

    What a jackass. I just thought you should know that some idiot is stealing your content and your traffic, trying to legitimize it with a weaksauce link to this (original) post.

    Cheers,
    Dave

  34. Brando Miranda says:

    I was wondering, you mention that `prefix o` changes to next pane. But is there such a thing as `prefix O` (capital o) such that it cycles in the opposite direction? I tried googling it wasn’t as easy to find as I thought it would have… but I did discover that `prefix arrow-keys` does something similar. But considering that the vim pane manager uses hjkl instead of arrows, I didn’t really like. Even if I did like it, its not the solution to the original problem anyway. Btw, Thanks for sharing ur knowledge! :D

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>