A Quick and Easy Guide to Tmux
Total Page:16
File Type:pdf, Size:1020Kb
7/24/2018 A Quick and Easy Guide to tmux Ham Vocke writing stu about software (/) Home (/) Blog (/blog) About Me (/about) Talks (/talks) (https://github.com/hamvocke) (https://twitter.com/hamvocke) (http://www.hamvocke.com/feed.xml) A Quick and Easy Guide to tmux 16 Aug 2015 terminal 13 minutes read I love working with the command line. Seriously, I think there’s hardly any more productive and more versatile tool for a software developer than the terminal. Maybe it’s the hacker/wizard/neckbeard kind of feeling I get when using a terminal, I don’t know. At work we do lots of pair programming. Everyone’s got their own laptop and can set it up the way they like. And since I love working with the command line I’ve spent quite some time doing (only sane! I swear!) modications to my terminal environment that make working with the command line more pleasant and streamlined. This is why my pair usually will be greeted by something like this: https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 1/21 7/24/2018 A Quick and Easy Guide to tmux If they’ve worked with me before they know what they are up to. But every once in a while there will be a new team member who doesn’t know my environment. Usually this is the point where they will ask something like “WTF am I looking at?” and it’s my time to shine! Because what they’re looking at is nothing less than the best thing since sliced bread. It’s tmux (https://tmux.github.io/), a so-called terminal multiplexer. Simply speaking, tmux acts as a window manager within your terminal1 and allows you to create multiple windows and panes within a single terminal window. https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 2/21 7/24/2018 A Quick and Easy Guide to tmux I’ll proceed to give them a two minute tour about what you can do with tmux. After that they’re either hooked and want to try it themselves or they tell me to go away with my ancient neckbeard tools and just use iTerm2. In the former case I’ll usually end up helping them installing tmux on their machine and will give them a 10 minute guide to learn the most important basics. This post will give you the same two minute introduction about tmux and its possibilities, followed by the typical 10 minute hands-on guide to set up and get to know tmux yourself. If you’ve got 10 minutes to spare and nally want to be more procient with tmux: read on! What’s tmux? tmux’s authors describe it as a terminal multiplexer. Behind this fancy term hides a simple concept: Within one terminal window you can open multiple windows and split-views (called “panes” in tmux lingo). Each pane will contain its own, independently running terminal instance. This allows you to have multiple terminal commands and applications running visually next to each other without the need to open multiple terminal emulator windows. On top of that tmux keeps these windows and panes in a session. You can exit a session at any point. This is called “detaching”. tmux will keep this session alive until you kill the tmux server (e.g. when you reboot)2. This is incredibly useful because at any later point in time you can pick that session up exactly from where you left it by simply “attaching” to that session. If you’ve ever worked with remote servers or a Raspberry Pi over ssh you can guess where this will be useful: When you lose your ssh connection the tmux session will simply be detached but will keep running on the server in the background including all the processes that run within your session. To continue your session simply ssh to the server again and attach to the running session. But tmux is not only helpful when working on a remote machine. Not only for its window management features but also for the session handling. Personally I nd myself detaching from sessions when I’m switching context. I’ll just start a new session for my new task and attach to the old session whenever I want to continue with my old task. https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 3/21 7/24/2018 A Quick and Easy Guide to tmux You see that tmux basically oers two big features: Window management in your terminal and session management. If you are familiar with GNU Screen (https://www.gnu.org/software/screen) this is nothing new. Think of tmux as an easier-to-use and a little more powerful alternative to Screen (obviously I’m being opinionated here). Enough with the talking already. Let’s get our hands ready in the hands-on guide! Getting Started This hands-on guide will get you up and running with tmux pretty quickly. It will only cover the basic features which should be more than enough to get started and be productive with tmux. Simply open your terminal and follow the instructions. Installation Fortunately installing tmux is pretty straightforward on most distributions a simple sudo apt-get install tmux (Ubuntu and derivatives) or brew install tmux (Mac) should be sucient. Starting Your First Session For your rst session simply start tmux with a new session: tmux This will create a new tmux session with a nice all-green status bar at the bottom: https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 4/21 7/24/2018 A Quick and Easy Guide to tmux The status bar is an important part of tmux. Apart from the currently opened windows (on the left) it also shows some system information like date and time (on the right). The status bar can also be customized and I’ve seen some really fancy stu around (upcoming calendar events, battery status, to name a few) but this is something we’ll leave for later. Splitting Panes Now that we’ve created our rst session we can get a feeling for panes. When you create a new session, tmux will by default start with one window and a single panel inside. We want a nice split-screen, so let’s split this bad boy. All commands in tmux are triggered by a prex key followed by a command key (quite similar to emacs). By default, tmux uses C-b as prex key. This notation might read a little weird if you’re not used to it. In this emacs notation C- means “press and hold the Ctrl key”3. Thus C-b simply means press the Ctrl and b keys at the same time. The shortcut to split panes into a left and a right pane is C-b % . Remembering what I’ve just told you about tmux’s sequence of prex and command key this means to split your single pane into a left and a right pane you press Ctrl and b at the same time, release both, and then type the % key. Voilà! You’ve just invoked your rst tmux command and split your pane in two. https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 5/21 7/24/2018 A Quick and Easy Guide to tmux Where there’s a split into left and right, there’s also a split into top and bottom pane. To split a pane into top and bottom panes use the C-b " shortcut. Navigating Panes Right now we’re trapped in the newly created pane. But we really really want to go back to the left one. Easy peasy: Switching to a dierent pane uses the C-b <arrow key> shortcut, where <arrow key> is the arrow key pointing to the pane you want to switch to. In our case we want to switch to the panel on the left so it’s C-b left for us. Just once more, so that we fully understand this: This means you press Ctrl and b (your prex) followed by the left arrow key to get to the pane on the left. You can now go ahead and split each of your new panels even further. Feel free to experiment and split your panes like a maniac to get a feeling for it. Closing Panes Closing a pane is as simple as closing a regular terminal session. Either type exit or hit Ctrl-d and it’s gone. Creating Windows Windows in tmux can be compared to creating new virtual desktops; if you’ve ever worked with one of the major Linux deskop environments (KDE, Gnome) you’ll hopefully nd this analogy helpful. https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 6/21 7/24/2018 A Quick and Easy Guide to tmux Creating new windows is as easy as typing C-b c (one last time: that’s Ctrl and b at once, then c ). The new window will then be presented to you in tmux’s status bar. You can now divide the pane in your new window as you like. Or don’t. That’s up to you. To switch to the previous window (according to the order in your status bar) use C-b p , to switch to the next window use C-b n . If you’ve created many windows you might nd it useful to go to a window directly by typing its number (the status bar will tell you which window has which number), just use C-b <number> where <number> is the number in front of the window’s name in your status bar.