Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Who Else Wants to Boost Their Terminal Productivity with tmux?

blog/cards/who-else-wants-to-boost-their-terminal-productivity-with-tmux.jpg

tmux allows you to save your terminal sessions and split your terminal into multiple windows. It's one of my favorite tools.

Quick Jump: What Does tmux Do? | Installation | Sessions | The Command / Prefix / Leader Key | Windows | Panes | Scrolling Up in the Buffer | Copy / Paste | Clocks and Cheat Sheets | Detaching From and Cleaning Up Sessions | Making tmux Your Own

When it comes time to get something done, the worst thing you can do is have barriers of entry that delay what you want to do. Often times, just getting started is the hardest part.

Here’s what life was like to write a blog post before I embraced tmux:

  1. Open a terminal
  2. Run cd ~/Development/Sites/nickjanetakis
  3. Open a new terminal tab
  4. Open a third terminal tab
  5. Switch to terminal tab 1
  6. Run subl . which launches my favorite code editor
  7. Switch to terminal tab 2
  8. Run chruby 2.4 to enable Ruby
  9. Hit CTRL-r to search my history for a long ass Jekyll command
  10. Run jekyll serve --config _config.yml,_config.dev.yml --livereload --drafts
  11. Switch to terminal tab 3
  12. Run cd _private/deploy/ansible to prepare deploying the site for later
  13. Switch back to Sublime Text 3 and create a new blog post file
  14. Begin writing the blog post

I had to do 13 steps just to get to the point where I can start writing. To make matters worse, I often compulsively close terminal windows after I am done. This is a process that gets repeated single every time I feel like writing.

It’s actually a huge barrier of entry. Sure they are trivial tasks, but they take up a whole lot of mental energy.

Living the good life is not all about money, it’s about avoiding stupid things like having to perform a tedious 30 second routine just to get started working on what you want to do.

This is where tmux steps in and it has nothing to do with writing blog posts , but in my case, with tmux I only run 2 commands to get started writing.

tmux will make you 17 trillion times more productive (those are my estimated stats btw) if you happen to use the terminal on a regular basis. It’s much more than the direct productivity gains by time saved too. When you tear down these barriers, they make you more happy and motivated to keep moving forward. This is the good stuff that you can’t measure, and you should take it whenever you get it, because it all adds up.

Here’s another example. Let’s say you started a successful freelance business and you’re drowning in clients. I mean, it’s gotten to the point where at 9am to 11am, you’re working with a client who requires some Docker and Ansible work, then your 1-3pm is helping to develop an MVP with Flask. On top of that, you’re writing blog posts and creating courses in the evenings.

That’s just Monday too. During the week you have other clients and before you know it, you’re juggling 6 projects at once for this month, each with their own tech stack, requirements and context.

Spoiler alert: This is what I do, but I’d love to hear what you’re up to. Let me know know in the comments at the end of this article.

If you shut things down between clients, it’s a monumental task to get things going again later. Or maybe disaster strikes, and after setting up half a dozen terminal tabs and have written a bunch of gnarly commands that you’ll need later, you fat finger the close terminal icon and you lose all of your terminal state, OOPS.

Instead of thinking “that stinks, time to re-do everything” like a rational human being, you goto Youtube and start browsing. It starts with looking for new music to listen to, and 4 hours later it ends with you in a cold sweat watching a half naked Japanese dude dance with a horse mask on while cooking mushrooms.

Ok, enough fun. Needless to say, tmux solves the above problems and if you take a few minutes to read this article you’ll learn enough about tmux to start using it effectively.

What Does tmux Do?

tmux has 2 main goals. One of them is to let you easily manage multiple terminal sessions in a single terminal window. The other is to persist sessions, so that if you close your terminal you won’t lose your state or progress (similar to what screen does if you’re familiar with that tool).

Installation

tmux is a program that you have to run. It’s available on Linux and MacOS. If you’re on Windows, you may want to consider using WSL or install a virtual machine for primary development purposes.

MacOS users can run brew install tmux to install it.

Linux users can install tmux with their distro’s package manager. I recommend installing at least tmux version 2.1 because it adds a lot of goodies.

For example, on Ubuntu 18.04 to install 2.6, you can run:

sudo apt-get install tmux

Now that things are installed, let’s start going over the terminology of tmux.

Sessions

You can think of sessions as distinct projects. They are something you create, and then attach to.

Personally, I have a “nj” session which holds all of my terminal state for my personal website. I created that by simply opening a terminal and running tmux new -s nj.

Then I have a bunch of different sessions for each project I am working on. For example, Scott is one of my clients (and friends), so I have a “scottj” session that has all of what we’re working on together.

When I want to work on something related to him I just open a terminal and run tmux attach -t scottj. Instantly, I’m ready to rock. All of my previous terminal information is restored, complete with bash history. It’s like I never left.

If you ever forget what sessions are available, all you have to do is run tmux ls to get a list of sessions.

Oh yeah, before we move on, it’s worth mentioning you can create unnamed sessions by just running tmux, in which case you can connect to them by number, which can be done like so: tmux attach #1. You already know how to figure out which sessions are available, right? Ok, just checking.

Go ahead and create a new session named “tmuxrox” now because the rest of this article will be adding things to that session. Remember, you learn best by doing, not passively reading. When was the last time you heard of someone learning how to ride a bike by reading a manual, and then actually being great at it the first time they hopped on?

The Command / Prefix / Leader Key

Now’s a good time to introduce the concept of the command key, which is also commonly referred to as the prefix or leader key. This is the key you would press to initiate any tmux keyboard shortcut. By default this command is bound to CTRL-b. Don’t worry, tmux has a config file and you can rebind it to something less annoying and we’ll cover that later.

For now, just understand the leader key is your gateway to run any tmux shortcut. There’s no lesson to practice it right now, but we’ll be using it a ton from this point forward.

Windows

Inside of each session, you can have 1 or more windows. You can think of them as tabs and if you’re following along and have created the “tmuxrox” session you’ll see a 0:bash* label on the bottom left. That is a window.

One of the ways you can create a new window is hitting the leader key followed by c. Try it now by hitting CTRL+b followed by c. You should now see a second window on the bottom which would be 1:bash*. Notice how the previous window is labeled 0:bash-. The * denotes the active window.

You can cycle between windows in a number of ways. You could do leader + n for the next window, leader + p for the previous window and leader + 1 (or any number) to jump directly to a specific window. Try creating 3 windows total and jump between the windows using all 3 shortcuts.

If you have a lot of windows and things are crowded you could also use leader + w to list all windows and then you can use the arrow keys and Enter to select one. If you get really lost in a sea of windows but you know the name of it, you can hit leader + f to find a window. Personally I don’t use this too often, but it’s good to know.

Speaking of names, wouldn’t it be nice if you could name the windows? tmux has you covered there with the leader + , shortcut. That will let you rename the active window. By default it is named bash or whatever shell you happen to use.

Try renaming all 3 windows so that you see the following:

  • 0:foo
  • 1:bar
  • 2:baz

Lastly, you can delete windows just as you would close a terminal normally. That means you can run exit or hit CTRL+d (no need for the leader key here). Go ahead and move to the baz window and delete it.

Panes

tmux would have been good enough if all we had were sessions and windows, but it gets much better. tmux also has panes which let you split a window into multiple independent terminals. This is my favorite feature of tmux.

The shortcut keys for that are leader + % and leader + ". Try them a few times on your own to see how they work. Pretty neat right? I know, the default shortcut keys are a little weird, but we’ll change them later.

Make sure you have at least 2 panes because the next thing we’ll cover is switching between panes. You can do that with leader + o. That will cycle through all of your panes. But if you have a few panes open, it can get tedious to cycle through them.

Try hitting leader + q to get a numbered label on top of each pane, then hit the number you want to switch to. This is a fantastic feature. In my own use cases, I have a few projects with 4 or 5 panes in 1 window and jumping to a number is something I do a lot.

When you have a few panes open, sometimes it’s useful to temporarily zoom into a specific pane without disrupting your pane set up. You can do that very easily by hitting leader + z in whichever pane you want to zoom. When you’re done, just hit the same combo again to revert things back to normal.

Try creating a few panes on your own and kill them by pressing leader + x. You can treat it like a game by selecting panes with the q feature and then kill them with x. Pew pew pew!

Scrolling Up in the Buffer

We’ve explored sessions, windows and panes but now it’s time to address the elephant in the room, and that’s “how in the world do I scroll up in the buffer?”.

Go ahead and hold down Enter for a few seconds and you’ll soon realize that your terminal’s scroll bar isn’t shrinking, even if you cannot view the entire contents of the buffer on 1 screen.

You could even hit Page up or Page down and it still won’t scroll, but don’t worry.

All you have to do is press leader + [, and after doing so you’ll see a numbered label in the top right of the pane which displays your current point in the buffer as well as the total number of lines.

Once you see that, you can begin scrolling up or down with the arrow keys or page keys. You can exit this mode by pressing ESC.

Scrolling up in itself is usually accompanied by copying text, so let’s cover that next.

Copy / Paste

Copying text is really weird with tmux, because you have to hit all sorts of non-standard key combos and then after you eventually copy your text, it’s only available to be pasted inside of tmux because it doesn’t get copied to your system’s clipboard. Clearly, that’s not very useful in your day to day as a software developer or sysadmin.

I recommend you just use your system’s clipboard. Normally in a terminal without tmux you could select the text you want with the mouse and then hit CTRL+Shift+c to copy whatever you want, followed by CTRL+Shift+v to paste it. With tmux, it will be no different except that you’ll need to hold down the shift key while you select your text.

If you have multiple panes open, it’s a good idea to zoom into a specific pane to capture only that 1 pane during the selecting phase. We covered zooming before.

In case you’re an original neckbeard (I salute you) and the thought of using a mouse makes you want to rip out my intestines and use them as guitar strings, then settle down because I’m going to explain how to copy and paste text with just tmux shortcuts next. No mice will be touched.

Ok, here’s how to copy and paste without the mouse:

  1. Hit leader + [ to enter “copy mode”
  2. Move the cursor to where you want to start copying
  3. Hit CTRL+Space
  4. Use the arrow keys (all directions work) to select the characters or lines you want
  5. Hit ALT+w to copy your text
  6. Hit leader + ] to paste the selection

I told you it was weird, but hey it works – even across windows and panes.

Clocks and Cheat Sheets

There’s 2 more keyboard shortcuts I want to mention before we start cleaning things up.

The first one is leader + t which shows a big ass clock. Since time is your most valuable asset, the tmux authors must have figured it was worth being able to tell time (hit ESC to clear it btw).

The last one is leader + ? which will show you every key combo that you can do. In case you ever forget what you can do, that is a built in cheat sheet.

Detaching From and Cleaning Up Sessions

At some point, you’ll certainly want to detach from a session so you can work on something else. You can do that by running leader + d, go ahead and try it now.

Lastly, if you ever want to delete a session, such as the “tmuxrox” session we created before, it’s as simple as opening a terminal and running tmux kill-session -t tmuxrox for named sessions. For unnamed sessions you would use tmux kill-session #1 or whatever number you want to kill.

Making tmux Your Own

So that covers getting familiar with tmux. You definitely learned enough to get going with it.

The next step would be to tweak its config so you can use key bindings that work best for you. You can also drastically change the appearance of everything and add your own custom toolbars to the status bar on the bottom.

If you want to my config file to get you started you can check it out below. It’s an accumulation of everything I learned about tmux while using it for quite some time.

It will set up more friendly key bindings, enable mouse support, and more.

Lastly, it will also set up tmux’s plugin manager and get you situated with a plugin that lets you resume tmux sessions, even if you reboot your computer. That is a must have feature in my opinion. Having to set up all of your sessions after a reboot is a massive time sink.

You can view the config file in my dotfiles repo on GitHub.

Thanks for reading.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments