vim user, you will be in for a treat.
Tip #1: Always Have a tmux Session OnGetting into a new habit is always an uphill battle. Just like trying to force myself into using the
hjkl keys in
vim by disabling the arrow keys, I needed to find a way to force myself to use tmux all the time. This is useful when you are in the middle of coding and you forgot that you should have tmux-ed in the first place.
The above example works with iTerm2 on Mac OSX. It shouldn’t be difficult to search for Linux equivalents. The main idea is to always run
tmux attach -t base || tmux new -s base when the terminal program starts up.
The line above attempts to connect to a previously created session called
base, or creates a new one (also called
base) if it doesn’t exist. You can rename
base to anything you want, it is not a special keyword. With this incantation, whenever you fire up the terminal, you will automatically connect back to your previous tmux session.
Tip #2: Multiple Pane SynchronizationThe video above shows four panes loaded with REPLs of four different languages. Going in a clockwise direction, we have Haskell, Ruby, Elixir, and Python. What
setw synchronise-panes gives us is that it simultaneously sends keyboard input from one pane to the rest of the panes. (In this case, we can also see the the Elixir REPL is far superior because of the colored output.)
Obviously, this demonstration is a mere party trick to impress developers and nothing more. A good use case is having to perform a repetitive operation on multiple machines. For example, imagine having a tmux session connected to four remote hosts and having to executer the exact same command on all of them.
Tip #3: Refreshing tmuxI am prone to clearing the terminal with
Command + L. Unfortunately in tmux, this just messes up the entire session. To illustrate, the above shows my typical tmux experience: I am happily programming when somehow, my fat fingers hit
Command+k and my entire tmux session gets messed up, making me mad:
Thankfully, the easy fix is to bind
prefix-r to reload the tmux configuration file, and everything is nice again. In
~/.tmux.conf, add the following entry:
bind-key r source-file ~/.tmux.conf
This tells tmux to source the configuration again, essentially reloading tmux.
Tip #4: The tmux Plugin ManagerThis is a recent discovery of mine. I didn’t realize someone had created a tmux plugin manager. I won’t bore you with the details of installation, since it is pretty straightforward. Once you get the plugin installed, it’s only a matter of selecting the plugin you want and pasting the entry into
If you use Vundle or Pathogen for
vim, the process is the same. You declare whatever plugins you want by supplying the GitHub username and repo, save
tmux.conf, then hit
prefix + I to install the plugin:
The next two tips cover two interesting plugins that can be installed with the tmux plugin manager.
Tip #5: Resurrecting tmuxtmux-resurrect is a tool to persist a tmux environment across system restarts. Why is this useful? When you restart tmux, your entire tmux session is gone.
tmux-resurrect fixes that. Another very nice feature of
tmux-resurrect is the ability to restore
vim sessions too!
In the video that follows, I have a
vim session in the left pane
rails console and
rails server, respectively, on the right.
In order for
tmux-resurrect to persist a
vim session, install Tim Pope’s vim-obsession.
vim-obssesion makes it easier to record a
vim session. I haven’t played with it much, but let me show you the entire process:
First of all, we have to tell
vim-obsession to track the session. I also have
htop running in another window.
In order to save an entire tmux session, type
prefix + Control + s. Then if something kills the tmux server, which is what happens when your battery runs out or when someone trips over the wire and the power gets cut. With
tmux-resurrect, losing your tmux session is not an issue. In order to restore the session, you need to run tmux again, and this time, hit
prefix + Control + r.
However, I can already here some people thinking, “Isn’t manually doing
prefix + Control + s + r is a hassle! Who has time to remember all that?” Enter tip #6.
tmux-continuum continuously saves your tmux environment at regular intervals and automatically restores it when tmux is started.
Tip #7: Zooming tmuxZoom was introduced in tmux 1.8. tmux zooming is very useful when you want to look at test failures or inspect logs. To zoom into a pane, hit
prefix + z, and use the same combination to zoom out.
Tip #8: Navigate Seamlessly Between
It can sometimes be confusing to figure out whether you are in a
vim Splits and tmux Panes
vim split or in a tmux pane. Navigating between both is also a hassle. That is until vim-tmux-navigator came along.
Here, we have a tmux session with three panes. The left half consists of two
vim splits, and the right hand side contains two tmux panes.
vim-tmux-navigator, I can navigate across
vim splits and tmux panes using
vim + tmux + Ruby Specs
vim and tmux make a very nice combination to run Ruby tests. For this to work, you need to install three
Micropost model. I have configured
to runs all the specs found in this file. However, when I hit
, it runs only the specs under the cursor. Extremely handy!
In addition to having an awesome name, tmuxinator is an excellent Ruby gem that helps you create an manage tmux sessions easily.
tmuxinator is especially useful when you have different projects requiring different layouts of panes/windows.
Thanks for Reading!I hope you enjoyed this article, and learned something new. Do you have other tips to share? Spread some tmux love in the comments below!
- tmux: Productive Mouse-Free Development by Brian Hogan
- Impressive Ruby Productivity with Vim and Tmux – Ancient City Ruby 2013 by Chris Hunt
Frequently Asked Questions (FAQs) about Tmux
What are the benefits of using Tmux over other terminal multiplexers?
Tmux is a powerful terminal multiplexer that offers several advantages over other similar tools. Firstly, it allows you to run multiple terminal sessions within a single window. This is particularly useful for developers who need to work on multiple tasks simultaneously. Secondly, Tmux sessions are persistent. This means that even if you accidentally close your terminal or lose your SSH connection, you can easily resume your work from where you left off. Lastly, Tmux is highly customizable. You can modify its configuration file to suit your specific needs and preferences.
How can I customize my Tmux configuration?
Tmux allows you to customize its behavior through a configuration file located at ~/.tmux.conf. You can modify this file to change various settings such as the key bindings, status line, color scheme, and more. For instance, you can change the prefix key from the default ‘Ctrl-b’ to something else like ‘Ctrl-a’. You can also set the default terminal size, enable mouse support, and customize the status line to display useful information such as the current time, date, and hostname.
How can I manage Tmux sessions, windows, and panes?
Tmux provides several commands to manage sessions, windows, and panes. You can create a new session using the ‘tmux new-session’ command or simply ‘tmux’. To detach from a session, you can use the ‘tmux detach’ command or the ‘Ctrl-b d’ key binding. To switch between sessions, you can use the ‘tmux switch-session’ command or the ‘Ctrl-b l’ key binding. Similarly, you can create, close, and navigate between windows and panes using various Tmux commands and key bindings.
How can I share a Tmux session with another user?
Tmux allows you to share sessions with other users, which can be useful for pair programming or collaborative debugging. To share a session, you need to create a new Tmux session and then change its permissions so that the other user can access it. The other user can then attach to the shared session using the ‘tmux attach-session’ command.
How can I save and restore Tmux sessions?
While Tmux sessions are persistent by default, they do not survive a system reboot. However, you can use a plugin like Tmux Resurrect to save and restore your Tmux sessions. This plugin allows you to save your current Tmux environment, including the sessions, windows, panes, and even the command history, to a file. You can then restore your Tmux environment from this file after a system reboot or whenever you need it.
How can I use Tmux for remote pair programming?
Tmux is a great tool for remote pair programming. You can create a Tmux session on a server and then share it with your pair programming partner. Both of you can then work on the same codebase in real-time, with each of you having the ability to write and execute code. This can greatly enhance your productivity and make the pair programming experience much more enjoyable.
How can I copy and paste text in Tmux?
Tmux provides a built-in copy-paste mechanism. You can enter the copy mode using the ‘Ctrl-b [‘ key binding, then navigate to the text you want to copy, and start the selection using the ‘Space’ key. After selecting the text, you can copy it using the ‘Enter’ key. To paste the copied text, you can use the ‘Ctrl-b ]’ key binding.
How can I split the terminal window into multiple panes in Tmux?
Tmux allows you to split your terminal window into multiple panes, either horizontally or vertically. You can create a horizontal split using the ‘Ctrl-b %’ key binding and a vertical split using the ‘Ctrl-b “‘ key binding. You can then navigate between the panes using the ‘Ctrl-b ArrowKey’ key bindings.
How can I resize panes in Tmux?
You can resize panes in Tmux using the ‘Ctrl-b Ctrl-ArrowKey’ key bindings. For instance, you can increase the size of a pane to the right by pressing ‘Ctrl-b Ctrl-RightArrow’. Similarly, you can decrease the size of a pane to the left by pressing ‘Ctrl-b Ctrl-LeftArrow’.
How can I run a command in all panes simultaneously in Tmux?
Tmux provides a feature called ‘synchronize-panes’ that allows you to run a command in all panes simultaneously. You can enable this feature using the ‘Ctrl-b :’ key binding followed by the ‘setw synchronize-panes on’ command. After enabling this feature, any command you type will be executed in all panes. To disable this feature, you can use the ‘Ctrl-b :’ key binding followed by the ‘setw synchronize-panes off’ command.