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.
Install
To install tmux, run the following brew command.
brew install tmux
Creating sessions
tmux works by creating sessions, you can create your first session by running the following command.
tmux new -s "hello-world"
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.
tmux detach-client
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.
tmux attach
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.
Window commands
tmux
commands are driven by the prefix key binding which is ctrl+b
. Press ctrl+b
(aka <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 alacritty.yml
.
key_bindings: - { key: E, mods: Command, chars: "\x02\"" } # split tmux window vertically - { key: E, mods: Command|Shift, chars: "\x02%" } # split tmux window horizontally - { key: T, mods: Command, chars: "\x02c" } # create a new tmux window - { key: W, mods: Command, chars: "\x02x" } # kill the current pane - { key: Tab, mods: Control, chars: "\x02n" } # switch to next tmux window - { key: LBracket, mods: Command|Shift, chars: "\x02p" } # switch to next tmux window - { key: RBracket, mods: Command|Shift, chars: "\x02n" } # switch to previous tmux window
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.
Configuration
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.
set -g base-index 1 # start indexing windows at 1 instead of 0set -g detach-on-destroy off # don't exit from tmux when closing a sessionset -g escape-time 0 # zero-out escape time delayset -g history-limit 1000000 # increase history size (from 2,000)set -g mouse on # enable mouse supportset -g renumber-windows on # renumber all windows when any window is closedset -g set-clipboard on # use system clipboardset -g status-interval 3 # update the status bar every 3 secondsbind-key x kill-pane # skip "kill-pane 1? (y/n)" prompt
To style the tmux status bar, I prefer a transparent, top-aligned bar, with minimal items display.
set -g status-left "#[fg=blue,bold]#S" # session nameset -g status-left-length 200 # increase length (from 10)set -g status-position top # macOS / darwin styleset -g status-right '' # blankset -g status-style 'bg=default' # transparent
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.
Plugins
tmux
plugins can be managed by tpm, the tmux plugin manager. To install it you first have to clone tpm to your machine.
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Add the following to your tmux config file.
# List of pluginsset -g @plugin 'tmux-plugins/tpm'set -g @plugin 'joshmedeski/tmux-fzf-url'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)run '~/.tmux/plugins/tpm/tpm'
Save the file and run <prefix>I
to install the tmux plugins.
Open URLs
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.
brew install fzf
<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 cmd+o
.
key_bindings: # ... existing keybindings - { key: O, mods: Command, chars: "\x02u" } # open URLs 'joshmedeski/tmux-fzf-url'
Conclusion
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.
Or use the