
Getting Started with tmux
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Book DescriptionThe book is intended for software developers, DevOps engineers, and other professionals who make heavy use of the terminal in their daily workflow. Some familiarity with the terminal is useful but no prior experience with tmux or other terminal multiplexers (such as GNU Screen) is required.What you will learn
Increase productivity by using tmux rather than a mouse to switch between terminal windows
Persist sessions on remote machines over SSH with tmux, making it easy to resume right where you left off even if your connection is terminated
Learn how tmux can be used to create persistent dashboards for monitoring servers
Use tmux to manage multiple terminal windows from a single one
Maximize terminal productivity with tmux
Maintain the state even when a terminal window is closed with tmux
Configure tmux and customize it for your needs
Who this book is for
All prices
More details
Other editions
Additional editions

Person
Victor Quinn, J.D., is a technology leader, programmer, and systems architect whose area of expertise is leading teams to build APIs and backend systems. Currently, he is building the API and backend system for SocialRadar, which is a startup that builds mobile apps that provide real-time information on people around you. Prior to joining SocialRadar, Victor led a rewriting of the financial processing online forms and APIs for NGP VAN, which is a company that processed billions of dollars in campaign contributions during the election year of 2012. The system he orchestrated is on track to process even more contributions in the coming election years. He led his team to build a system that included auto-filling and a sign-on system, enabling future contributions with a single click. All of these features were rolled up in a JavaScript single page app, making a fully functional payment processing form embeddable into even a static web page with a single tag. He has spent many years honing his skills with command-line tools such as tmux in order to be maximally efficient in his work. His editor of choice is Emacs and he uses the Dvorak keyboard layout. He has Bachelor of Science degrees in Physics and Computer Science from the University of Massachusetts Amherst and a Juris Doctor with focus on Intellectual Property Law from Western New England University. He is an Eagle Scout and a registered patent agent. He lives in the Washington, D.C., metro area with his wife and Great Dane and enjoys brewing his own beer and riding his Harley.
Content
Jump Right In
Configuration
Sessions and Windows
Text Manipulation
Advanced Usage
Tips and tricks
Other tools used with tmux
Appendix
Naming the session
Each session has a name that you can set or change.
Notice the [0] at the very left of the status bar? This is the name of the session in brackets. Here, since you just started tmux without any arguments, it was given the name 0. However, this is not a very useful name, so let's change it.
In the prompt, just run the following command:
$ tmux rename-session tutorialThis tells tmux that you want to rename the current session and tutorial is the name you'd like it to have. Of course, you can name it anything you'd like. You should see that your status bar has now been updated, so now instead of [0] on the left-hand side, it should now say [tutorial]. Here's a screenshot of my screen:
Of course, it's nice that the status bar now has a pretty name we defined rather than 0, but it provides many more utilities than this, as we'll see in a bit!
It's worth noting that here we were giving a session a name, but this same command can also be used to rename an existing session.
The window string
The status bar has a string that represents each window to inform us about the things that are currently running. The following steps will help us to explore this a bit more:
- Let's fire up a text editor to pretend we're doing some coding: $ nano test
- Now type some stuff in there to simulate working very hard on some code:
First notice how the text blob in our status bar just to the right of our session name ([tutorial]) has changed. It used to be 0:~* and now it's 0:nano*. Depending on the version of tmux and your chosen shell, yours may be slightly different (for example, 0:bash*). Let's decode this string a bit.
This little string encodes a lot of information, some of which is provided in the following bullet points:
- The zero in the front represents the number of the window. As we'll shortly see, each window is given a number that we can use to identify and switch to it.
- The colon separates the window number from the name of the program running in that window.
- The symbols ~ or nano in the previous screenshot are loosely names of the running program. We say "loosely" because you'll notice that ~ is not the name of a program, but was the directory we were visiting. tmux is pretty slick about this; it knows some state of the program you're using and changes the default name of the window accordingly. Note that the name given is the default; it's possible to explicitly set one for the window, as we'll see later.
- The symbol * indicates that this is the currently viewed window. We only have one at the moment, so it's not too exciting; however, once we get more than one, it'll be very helpful.
Creating another window
OK! Now that we know a bit about a part of the status line, let's create a second window so we can run a terminal command. Just press Ctrl + b, then c, and you will be presented with a new window!
A few things to note are as follows:
- Now there is a new window with the label 1:~*. It is given the number 1 because the last one was 0. The next will be 2, then 3, 4, and so on.
- The asterisk that denoted the currently active window has been moved to 1 since it is now the active one.
- The
nanoapplication is still running in window 0. - The asterisk on window 0 has been replaced by a hyphen (-). The - symbol denotes the previously opened window. This is very helpful when you have a bunch of windows.
Let's run a command here just to illustrate how it works. Run the following commands:
$ echo "test" > test $ cat testThe output of these commands can be seen in the following screenshot:
This is just some stuff so we can help identify this window. Imagine in the real world though you are moving a file, performing operations with Git, viewing log files, running top, or anything else.
Let's jump back to window 0 so we can see nano still running. Simply press Ctrl + b and l to switch back to the previously opened window (the one with the hyphen; l stands for the last). As shown in the following screenshot, you'll see that nano is alive, and well, it looks exactly as we left it:
The prefix key
There is a special key in tmux called the prefix key that is used to perform most of the keyboard shortcuts. We have even used it already quite a bit! In this section, we will learn more about it and run through some examples of its usage.
You will notice that in the preceding exercise, we pressed Ctrl + b before creating a window, then Ctrl + b again before switching back, and Ctrl + b before a number to jump to that window.
When using tmux, we'll be pressing this key a lot. It's even got a name! We call it the prefix key. Its default binding in tmux is Ctrl + b, but you can change that if you prefer something else or if it conflicts with a key in a program you often use within tmux. You can send the Ctrl + b key combination through to the program by pressing Ctrl + b twice in a row; however, if it's a keyboard command you use often, you'll most likely want to change it. This key is used before almost every command we'll use in tmux, so we'll be seeing it a lot.
From here on, if we need to reference the prefix key, we'll do it like <Prefix>. This way if you rebind it, the text will still make sense. If you don't rebound it or see <Prefix>, just type Ctrl + b.
Let's create another window for another task. Just run <Prefix>, c again. Now we've got three windows: 0, 1, and 2. We've got one running nano and two running shells, as shown in the following screenshot:
Some more things to note are as follows:
- Now we have window 2, which is active. See the asterisk?
- Window 0 now has a hyphen because it was the last window we viewed.
- This is a clear, blank shell because the one we typed stuff into is over in Window 1.
Let's switch back to window 1 to see our test commands above still active. The last time we switched windows, we used <Prefix>, l to jump to the last window, but that will not work to get us to window 1 at this point because the hyphen is on window 0. So, going to the last selected window will not get us to 1.
Thankfully, it is very easy to switch to a window directly by its number. Just press <Prefix>, then the window number to jump to that window. So <Prefix>, 1 will jump to window 1 even though it wasn't the last one we opened, as shown in the following screenshot:
Sure enough, now window 1 is active and everything is present, just as we left it. Now we typed some silly commands here, but it could just as well have been an active running process here, such as unit tests, code linting, or top. Any such process would run in the background in tmux without an issue.
Tip
This is one of the most powerful features of tmux.
In the traditional world, to have a long-running process in a terminal window and get some stuff done in a terminal, you would need two different terminal windows open; if you accidentally close one, the work done in that window will be gone.
tmux allows you to keep just one terminal window open, and this window can have a multitude of different windows within it, closing all the different running processes. Closing this terminal window won't terminate the running processes; tmux will continue humming along in the background with all of the programs running behind the scenes.
Help on key bindings
Now a keen observer may notice that the trick of entering the window number will only work for the first 10 windows. This is because once you get into double digits, tmux won't be able to tell when you're done entering the number. If this trick of using the prefix key plus the number only works for the first 10 windows (windows 0 to 9), how will we select a window beyond 10?
Thankfully, tmux gives us many powerful ways to move between windows. One of my favorites is the choose window interface.
However, oh gee! This is embarrassing. Your author seems to have entirely forgotten the key combination to access the choose window interface. Don't fear though; tmux has a nice built-in way to access all of the key...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.