Tmux is an amazing tool. It’s a “Terminal MultiPlexer” that helps you manage multiple Terminal windows very quickly and easily. You can even save entire work environments that have multiple windows open, so instead of having to setup your work environment every time, you can just hop into your pre-setup environment. Ready to get started?
I’m going to leave installation up to you. You’ll probably want to use your system’s package manager, so HomeBrew for mac, or.. apt for Debian/Ubuntu or whatever system you use.
Once Tmux is installed, run the following in your terminal to start tmux:
You’ll get something like this. Notice the Green bar at the bottom.
Reading Makes You Smarter!
Check out our recommended reading! We've chosen a variety of books covering a lot of different topics from Security, Functional programming, clean code, design, Computer Networking and more.
Tmux has a Prefix key that is typed before every command. By default, the Prefix key is C-b (Press Control and b at the same time)
to see all commands available, check out the Tmux Manual, or type C-b ?
To split the terminal window into two separate top-bottom “panes”, type C-b “ (remember to use shift when typing “). The command to split windows Left-to-Right is C-b % (again use shift when typing %). To Switch between the visible panes, type C-b arrow-key Where arrow-key is the direction you want to move. You can go up, right, left, or down… Obviously.
Go ahead and try splitting windows both up, down, left and right. Also move to different panes and split them however you desire. I went a tad bit crazy, and this is what I ended up with:
To CLOSE a pane, type Ctrl+d or exit in the pane you want to get rid of. Keep a couple panes open so you can see visually the difference between Panes and Windows.
Another way to split things up is through Windows. You can have multiple Panes inside each window. To create a new “Window” type C-b c and a brand-new full size “Window” should appear. C-b n moves to the NEXT window, while C-b p moves to the PREVIOUS window. If you have more than two windows, then C-b number will select the specified window.
Note: The Status bar on the bottom will display the windows open and a number to identify each window. See the 0, 1, and 2 below?
Create 3 or 4 windows, and try to jump to them using C-b p, C-b n, and C-b number. The * indicates which window is currently visible. You can also name windows to make it easier to know what that window has inside with the rename function: C-b , I renamed 3 windows. One “cats”, one “dogs”, and one “cake”.
When you first start up Tmux with $ tmux, you are starting a “Session”. All Panes and Windows live inside of a session. You probably don’t want to NEST sessions, so be careful not to start a session inside of a session (A.K.A Don’t run $ tmux inside Tmux.
One way to do this is to create ONE session, and have your entire development environment split up into Windows and Panes within that session, then name that session “your-project-name” or… whatever. But hey, you do things how you want.
If you exit or close all of the windows and panes inside a session, the session will be destroyed.
Run the following command to view all the current sessions running:
$ tmux ls
As you can see above, I have ONE session, and that session has 1 window inside of it. There’s also (Attached) at the end, meaning I am currently “attached” to this session. If you are attached to a session, DETACH the current session with C-b d. Now run the terminal command $ tmux ls again to see the session still exists (You’re just not attached to it!).
If everything worked you should be OUTSIDE of Tmux and back into your normal terminal. We can now re-enter this session, or create a new separate session. Let’s create a new session by running $ tmux again (When you’re NOT attached to any sessions).
Now if we run $ tmux ls again, we should see TWO sessions.
To detach from the current session, type C-b d. Attach to the session you want with $ tmux attach -t the-session.
For me, the session 0 is the one with 3 windows. if I wanted that one, I’d just run $ tmux attach -t 0 and Boom, you’re in!
If you accidentally close out of your terminal, you don’t have to setup your whole environment again. The session is saved and you can just hop right back in by re-attaching to the session.
to KILL sessions, you run $ tmux kill-session -t session-to-kill. This will destroy all terminal panes and windows in that session, and the session itself.
Having helpful names is important. so instead of naming sessions 0, 1, 2, etc… Give them meaningful names. You can set up your entire dev environment on a per-project basis via sessions.
If you’re creating a session from scratch, you can run $ tmux new -s myRubyProject as an example to create a session called myRubyProject.
You can also re-name existing sessions with $ tmux rename-session -t oldName newname
Want More Tutorials?
Subscribe to our NewsLetter to get our latest Tutorials, Courses, product & tool reviews, and more! We don't email very often. When we do, it'll be good!
Save Sessions for later!
One of the best things about Tmux is that you can save your sessions for later! every time you start up your environment for the day, you can hop right into your complete environment without
having to set everything up every single time.
Note: Things change, so I recommend following the README’s install instructions over mine.
As of writing this, “Tmux Plugin Manager” handles all Tmux plugins for us, so we only need to install that.
Clone the Tmux Plugin Manager repository into the ~/.tmux/plugins/tpm directory. (Create it yourself if it doesn’t exist!)
$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Now we just need to RUN this plugin at the bottom of our ~/.tmux.conf file and use it to install tmux-resurrect. Create ~/.tmux.conf if it doesn’t already exist, and add the following to the BOTTOM of the file:
# List of Plugins set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-resurrect' # run the tmux plugin manager plugin run '~/.tmux/plugins/tpm/tpm'
Exit Tmux and restart. Then run prefix + I (Capital i) (prefix by default is C-b) to install tmux-resurrect! That was it. You can now save your sessions, and they’ll be ready for you even after you restart your computer!
Setup your Tmux sessions any way you like, and save them with prefix + Ctrl-s . Then whenever you want to pop into your tmux environment, start Tmux and run prefix + Ctrl-r to restore all of the sessions. Then attach to the session you want! No more setting up a half dozen terminal windows!
Optional: Alias For Text Editor
If you run a command to open your text editor within the terminal, the editor automatically opens up in the “present working directory”. I like to use an alias to quickly open an editor, and because Tmux restores the sessions to the right directory, I can just run a command to open the editor in the project directory.
I use Emacs, so I have this alias setup in my ~/.bashrc file:
alias em='nohup emacs &>/dev/null &'
this allows me to run $ em which opens up Emacs in the background in the directory that I run the command in. So The moment I pop into my Tmux session I run $ em and I’m right where I need to be in my text editor.
Now we just need to setup better keybindings because the defaults suck. To be brief, Here’s just a couple simple setups. I explain via comments
Note: M- is “Meta”, which is usually ALT
# remap prefix from 'C-b' to 'C-a' unbind C-b set-option -g prefix C-a bind-key C-a send-prefix # split panes left-to-right with | and top-to-bottom with - bind | split-window -h bind - split-window -v unbind '"' unbind % # switch panes using Alt-arrow without prefix # VIM users should consider using H, J, K, and L # I don't because I use a Tiling Window Manager, and they're already taken. bind -n M-Left select-pane -L bind -n M-Right select-pane -R bind -n M-Up select-pane -U bind -n M-Down select-pane -D # don't rename windows automatically set-option -g allow-rename off # List of Plugins set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-resurrect' run '~/.tmux/plugins/tpm/tpm'
If you made it through this tutorial, you should have a very strong start with Tmux. You’ve learned how to customize your keybindings, and how to manage windows, sessions, and panes, and even how to setup an entire development environment for re-use. There’s a lot of different ways people customize Tmux, so be sure to check out other people’s setups and plugins. Have fun!