This is a quickstart guide for tmux. My goal is to show you a few of the things tmux can do, and tell you just enough about how it works to give you a conceptual framework to hang the details on as you encounter them. Assuming you have tmux installed, you launch it from the terminal by typing 'tmux'. At first glance, this doesn't do very much -- the only difference you can see is that your terminal now has a status bar across the bottom. When you type 'exit', you get back to your original shell session. There's more going on here, though. I'll do that again, only this time I'll also bring up a list of running processes. I've filtered that list to show only processes named 'tmux', and this time, when I run 'tmux', you can see that it starts not one, but two processes. One of these processes is a server that runs as a daemon. When the server starts up, it launches a shell and keeps it open. The other process is a client that runs in your terminal. When you type into your terminal, tmux forwards your keystrokes along to the server, and the server forwards them to its shell. Likewise, any output from that shell is forwarded from the server back to the client, and from the client to your terminal. Already, this lets us do something cool. I can launch vim, start editing some files, and then just close my terminal window. Later, I can launch a new terminal window, type 'tmux attach', and there's my vim session, just as I left it. This ability to leave a process running comes in very handy if you need to kick off a slow job on a server, but you can't stay connected for the whole time it runs. It's also very useful if you're trying to work over a flaky Internet connection. -- Being able to reattach to an existing session is nice, but tmux can do a lot more. Let's talk about why it's called tmux. The 't' is for terminal, and the 'mux' is short for multiplexer. Multiplexing means a lot of different things, but the thing that most of its usages have in common is that they combine several things together so they can be treated as one. Tmux does this in two different ways: it can combine multiple shells into one tmux session, and that tmux session can be shared by more than one client. Before I can show you how that works, I need to take just a minute to talk about tmux's interface. As I mentioned earlier, the tmux client forwards your keystrokes to the tmux server. For the most part, this makes tmux relatively transparent: you do whatever you would normally do in a terminal. However, there are times when you want to control tmux itself. To allow you to do this, tmux is always listening for a special command character. tmux calls this the prefix character, and by default, it's Control-B. When you press Control-B, tmux *doesn't* send that keystroke along to the server right away; instead, it waits for the following character to decide what to do. For example, I don't have to close my terminal window to exit the client. I can hit Control-B, and then press 'd' to 'detach' from the current tmux session. This drops me back to the original shell session, where I can run 'tmux attach' to get back in. tmux comes with a default set of keybindings that's rather large, and you can add your own in a configuration file as well. There are more of them than I have time to go into in this screencast, but you can view them all by pressing the prefix key -- that's Control-B -- and then the question mark. This brings up a list of all of the current keybindings. When you're done, you can type Q to get out. Okay. Now for the fun stuff! Remember how I said that tmux can combine multiple shells into one tmux session? Let's talk about that for a moment. Right now, the status bar along the bottom has this "0:vim*" thing on the left. This is tmux's way of telling me that I have one window. (A window is like a screen; it has input and output, and takes up the full area of your terminal.) To open a second window, I can type (( PREFIX c )). That's 'c' for 'create' -- a lot of the commands have mnemonics to make them easier to remember. Now that I've done that, notice that I'm not looking at vim anymore. Also, the status bar along the bottom now has a second entry on the left; this is telling me that I have two windows, each of them with a number. The asterisk indicates which window is currently active. I'll run 'ping google.com' in this window, then open a third one using (( PREFIX c )) again. This is fun, but obviously I might want to switch back to one of those other windows. I can do that in a few different ways. I can do (( PREFIX n )) to go to the next window, and (( PREFIX p )) to go to the previous window. (note that previous and next wrap around!) I can also jump directly to a window numbered zero through nine, by typing PREFIX followed by the number of the window I want to go to. I'll do (( PREFIX 1 )) to get back to that ping session so I can stop that. tmux also lets you split a window into panes, resize the panes, move panes around, move panes into their own windows and move windows back into panes, and all kinds of craziness. I'm not going to go into all of that in this screencast; if you're interested, you can check out the book "tmux: productive mouse-free development" by Brian P. Hogan, available from the Pragmatic Bookshelf. (I don't get any money from them; I just like the book.) However, there is one more thing you need to know about. Because tmux takes over your terminal I/O, it breaks the scrollback features built into your terminal program. If I scroll up right now, I get garbage. This can be really annoying if you're trying to page through a lot of output, so the last thing I'm going to show you before I move on is how to scroll back in tmux. In tmux, scrollback is part of what's called "copy mode." To get into copy mode, you type the prefix key, followed by the left square bracket. (Yeah, I know -- it's not very intuitive. You get used to it. Or, you bind it to something that makes more sense to you.) Anyway, once you've done that, the only indication you get that anything has changed is a little status overlay at the top right of the tmux session. Once you're here, you can use the arrow keys to scroll up. You may be wondering why this is called "copy mode." Once you're here, you can press the space bar to start highlighting text, and then press Enter to copy it to a paste buffer that lives on the tmux server. Once you hit Enter, tmux exits copy mode and goes back to the current window. Later, you can have tmux paste it back for you by typing the prefix key, followed by the *right* square bracket. Again, this might come in handy if you're working across a flaky connection, but most of the time I don't bother with this. I'm just talking about it now so that you'll remember that tmux has a thing called copy mode, and that copy mode is what you use to scroll back. -- We've seen how tmux can combine multiple shells into one tmux session. Let's look at the other thing that makes tmux special, which is the ability for more than one tmux client to share the same tmux session. I use this almost every day for pair programming. Sometimes my pair is sitting just a few feet away, and sometimes there's a three-hour time difference between us. I've even had half a dozen people signed into my machine just to show them how this works. I don't really want to haul out another computer and record its screen as well, so you'll have to use just a bit of imagination here. I'll open a second terminal window alongside the first, and just type 'tmux attach'. Now, I can type something and see my changes reflected in both screens. I'm running all of this on my laptop, but the other tmux client could just as easily be piped across an SSH connection to somebody else's machine. Here's an important thing to know if you're going to use tmux for pair programming: right now, I'm using two terminal windows that have exactly the same dimensions. However, one of the things the tmux client does in the background is keep track of the dimensions of the terminal that's attached to it. The working area available to a given tmux session is the smallest screen that's currently attached. If I drag one of these windows around to make it smaller, the other window gets a border of dots around it to match. I like a lot of real estate when I work. I mean A LOT. I've bumped my laptop's resolution down to record this screencast, but I usually run about 7.5 megapixels, and I'd run more if I could figure out how to fit the screens on my desk and not burn up my GPU. So when I use tmux for pair programming, I make my terminal full screen on my largest monitor, and shrink the font down to the smallest size I'm comfortable with, and I ask my pair partners to do the same. -- That's it for this crash course. I've only covered what I think are the absolute essentials you need to get started using this great open-source tool. For more information, you can check out the repository I put together for work at github.com/livingsocial/ls-pair. This has a standardized tmux config file with some useful keybindings, some cheat sheets for tmux and vim, and a README with some more advice about remote pair programming in general. Also, if you're on a Mac, there are some command-line scripts you can use to make it easier to quickly set up accounts for remote pair partners. I also mentioned the tmux book earlier; much of the standardized tmux config file I put together was swiped from that book. It's a very short book I flipped through in an afternoon, and it's well worth the money. You might also want to check out these related projects: * wemux makes it easy to share a tmux session between different user accounts, so you can pair with other people without giving them access to all of your files. * tmuxinator helps you set up complex tmux workspaces using a config file. * And, of course, check out pairprogramwith.me for more resources on remote pair programming. Last but not least, I'd love to hear your feedback. I have a very search-engine-friendly name, and I'm 'geeksam' on Twitter, GitHub, and GMail. Pull requests to ls-pair are extremely welcome. A special thanks to everyone who offered early reviews and fact-checking. Thanks for watching, and happy hacking!