Manage Terminal Sessions with tmux
tmux is a terminal multiplexer that allows you to create and manage multiple terminal sessions within a single window. It enables you to run multiple programs or shell sessions simultaneously, switch between them easily, and detach and reattach sessions, even if you disconnect from the terminal.
To install tmux, run the following brew command.
tmux works by creating sessions, you can create your first session by running the following command.
You’ll notice a green bar at the bottom of your terminal with the words
hello-world to the far right, this is the tmux status bar!
To detach from tmux, run the detach command.
You’ll notice the green bar at the bottom disappears, but tmux is still running in the background! To reattach, you can run the attach command.
And just like that, you will end up back where you were before. Detaching from tmux sessions becomes very powerful as you create multiple sessions at the same time. Unlike graphical applications, like VSCode, tmux runs like a server and allows you to switch between different sessions quickly and easily with very little overhead on your computer. We’ll get more into that later.
tmux commands are driven by the prefix key binding which is
<prefix>) and then
c to “create” a tmux window. Think of these as macOS tabs that are common in browsers and file managers.
Windows can then be split into panes with
<prefix>% and killed with
<prefix>x. Note, when there are multiple panes the kill command will close the active pane, if there is only one pane the kill command will close the window entirely and switch to the previous window in the session, if there are no windows left it will kill the entire session.
The prefix key is my least favorite feature of tmux. Thankfully, Alacritty allows us to create custom keybindings to recreate common macOS-style shortcuts to make tmux much easier to use. Add the following to your
Give these a try! These familiar shortcuts, like
cmd+t will create a new tmux window and
ctrl+w will close it. I have a whole video explaining all my macOS Keyboard Shortcuts for tmux if you want to learn more about how this works.
I think tmux isn’t very pretty by default, and there are multiple options I prefer to change. All of tmux’s configuration is stored in a
~/.config/tmux/tmux.conf file. Create it and add the following options.
To style the tmux status bar, I prefer a transparent, top-aligned bar, with minimal items display.
The session name is style using
#[fg=blue,bold] to make it blue and bolded to match our starship configuration and create a more consistent look.
tmux plugins can be managed by tpm, the tmux plugin manager. To install it you first have to clone tpm to your machine.
Add the following to your tmux config file.
Save the file and run
<prefix>I to install the tmux plugins.
Notice in the snippet above a
joshmedeski/tmux-fzf-url plugin was added. This plugin allows you to open URLS visible in your tmux buffer. Once installed, you will need fzf installed to use it.
<prefix>u will open a fzf window that lists all available URLs that can be opened. Including GitHub style strings (“username/repo”)! But as mentioned earlier, you can add an Alacritty keybinding to map this command to
tmux is extremely powerful. We covered the basic commands for creating sessions, configuring options, setting up macOS shortcuts with Alacritty, and adding your first plugin. There is much more to cover so I recommend checking out my other content on tmux, like Smart tmux sessions with zoxide and fzf . You can also run
man tmux in the terminal to open the tmux manual and dive deep into everything tmux has to offer.
Sign-Up for New Posts
Stay in the loop and get the latest blog posts about dotfiles sent to your inbox.