Sort — tmux: Power up your shell
A few years back while working for @FamilySearch I had a chance to attend their internal conference, Sort. During this conference there was a session discussing the tool tmux which I had heard about, but wasn’t very familiar with. It was a really insightful look into how to increase your productivity without the need for a mouse.
Historically I have used screen and byobu to accomplish a similar function while remotely connected to a linux server. However I have come to love using tmux as a tool in my everday work as a developer.
Tmux is a terminal multiplexer. If you are a heavy user of command line execution on nix-like environments (including ssh) than you will want to become comfortable with using terminal multiplexers. This will allow you to be able to persist sessions, allow for “remote pair-programming” like features.
To work with tmux you need to realize that each tmux run creates a “session”. Each session can intern have multiple windows. And each window has 1+ panes. When a pane is created it is a completely new shell environment and does not keep aliases or other configuration applied to a previous pane.
The other most important aspect of tmux is how to send commands. By default this is adjustable but is defined by first hitting the below key combination followed by the action you would like to take.
Session can be created using names which can allow you to easily switch between multiple sessions depending on work you are doing. To work with session information you start by using
This will create a new unnamed session, which you can rename using the bind key followed by ‘$’. If you want to create a session from scratch with the correct name you can use
tmux new-session -s <name>
You can now disconnect from a session using the bind key followed by ‘D’. After disconnecting, reconnecting is accomplished using
tmux attach -t <name>
We have a newly created session and a single window and a pane at this point. So lets go over how to work with windows, both creating and navigation.
To create a new window from inside the session you can use the default shortcut ‘c’. This will create a new window that will default to the window number that has already been created. You can also create a new window for an existing session using the following command line call.
tmux new-window -t <session-name>
If you want to name this window you can either name it using the shortcut ‘,’. You can also name it at creation or rename from command line using one of the below commands.
tmux new-window -t <session-name> -n <window-name>
tmux rename-window -t <session-name>:<window-number> <window-name>
So we have window creation and renaming, now on to window destruction. The shortcut for destroying the current window is ‘&’. This will prompt you before the deletion will be completed, it will also terminal all running processes that were started in any of the windows open panes.
Window navigation is also pretty straight forward. The below list is the shortcuts and can be used when navigating.
- bindkey + n — Navigate to the next window
- bindkey + p — Navigate to the previous window
- bindkey + w — Open up a window chooser
- bindkey + [0–9] — Quickly navigate to selected window [0–9]
This is (of course) the reason we are using tmux. The ability to work with terminals. A pane is always its own terminal. Up until now we have basically defined tools used to organize these created terminals, but panes are the only ones that are terminals.
Pane creation is done through window manipulation. So you always start with your first pane, to create a second you must SPLIT the existing pane. Splitting the pane can be down either vertically or horizontally.
- bindkey + % — Split pane 50% horizontally
- bindkey + “ — Split pane 50% vertically
- bindkey + Ctrl-ArrowKey — Resize currently selected pane
- bindkey + ArrowKey — Navigate between panes
Tmux also has a file that you can use to customize the running configuration (this includes changing the bind key or any other operation defined above). This file lives in your home directory and is called .tmux.conf. I have an example of a configuration that can be used below.
The last, and probably most exciting, portion of tmux, is the ability to script the creation of the environment that you are working on. I find that there are some situations I run into where I would like to have multiple windows open for different actions. A common scenario is to have one pane open that has my unit test runner that auto runs with file changes, and the second pane have vim open on the project.
Scripting of tmux is fairly straight-forward, so rather then going through the details I have attached this gist that can be used to create this exact situation.