In part 1 I went over the basics of tmux and how to utilize its basic features. In this portion I’m going to dive a bit more into customizing tmux to make it easier and prettier to work with. I’ll also give some examples on how to utilize the client/server model.
Since this part is less of a tutorial and more of a tips / reference I decided a table of contents based layout would be a bit easier to use:
Note: If you have any tips, tricks or pre-made configurations you want to contribute to this post please do so in the comments and I’ll append them to this post.
Note**: You can type the commands into a live tmux session or place them in ~/.tmux.conf file which is automatically loaded when tmux is started.
Modify tmux bindings
- Rebinding the action key (Ctrl-b)
- Bind a key to switch between last active window
- Rebinding pane splitting bindings
Modify tmux looks & style
Misc tips & tricks
- Setting up your tmux environment via shell scripting
- Notify you when a window has activity
- Have tmux rename window automatically on command run
Modify tmux bindings
By default tmux has a decent default layout for hotkeys however there are a few keys that either made my fingers contort into ways they shouldn’t have or keys I simply always forgot. Luckily tmux offers a very simple syntax to rebind any key.
Rebinding the Action Key
Here is how you can rebind the Ctrl-b prefix to be something a bit easier to type. I personally use Ctrl-a similar to screen:
set-option -g prefix C-a
Note: The “-g” switch stands for global so the binding will affect every window. Alternatively the set-option command accepts arguments to specify the session and target of that particular binding if you like.
Now every subsequent command will be prefixed with Ctrl-a instead of Ctrl-b.
Binding a key for “last-window”
One of my favorite screen hotkeys is the last window hotkey which allows you to quickly switch between the current window and last window that was active. By default tmux doesn’t have a binding for this however it can easily be achieved by using the following:
bind-key C-a last-window
What this does is binds Ctrl-a to switch between the last active window. To use this binding you would hit Ctrl-a twice (since the prefix is set to Ctrl-a and the binding Ctrl-a).
Rebinding the pane splitting bindings
One of the most powerful features of tmux is the ability to natively split your windows into several panes. Unfortunately the default bindings to achieve this are simply unintuitive. As a result I’ve rebound the splitting commands to something I could remember:
unbind % # Remove default binding since we’re replacing
bind | split-window -h
bind – split-window -v
At first this may look odd but for me it’s easier to remember that “|” splits the screen vertically while “-“ splits the screen horizontally. To achieve the below window structure you would type the following sequence:
The first Ctrl-a |command splits the current window in half. The active window is the pane on the right so the subsequent Ctrl-a - splits that window into two vertically. Hitting Ctrl-a - once more now splits that active window into two more by splitting it vertically.
Note: Depending on the active layout you’re using the behavior of how the windows split may be different.
Modify tmux look & style
The default colors and style of tmux isn’t awful however it’s somewhat bland for my liking. I personally only have modified tmux slightly however it allows you to do quite a bit. In this section I’ll go over some of tmux’s style features.
Modifying tab color & looks
By default the tab colors are pretty bland and it makes it difficult to distinguish the active window from the other windows. Here is the default tmux tabs vs. the modified ones (snippet below):
# Set status bar
set -g status-bg black
set -g status-fg white
set -g status-left ‘#[fg=green]#H’
The first two commands set the background to black and the text to white. The third command is where the magic happens: the status-left command tells tmux to display the following text to the left of the terminal. The [fg=green]#H portion tells tmux to display the hostname of localhost and make it green. The #H portion is part of tmux variable expansion – please refer to the man pages for more information on other ones you can use.
That alone doesn’t account for highlighting the active window. If you want to do that use the following snippet:
# Highlight active window
set-window-option -g window-status-current-bg red
The set-window-option -g window-status-current-bg red command tells tmux to change the background of the current active window to red. The set-window-option command has several other options you can pass to it to achieve similar affects.
Adding information to your session
Sometimes it might be useful to add some information from your local machine to the tmux screen. Earlier we played with the status-left command which sets the left portion of the status bar. Let’s use the status-right command to add some information to the right side:
As you can see I’ve added the amount of users logged in and the current load average for my computer. This was achieved with the following snippet:
set -g status-right ‘#[fg=yellow]#(uptime | cut -d “,” -f 2-)’
Similar to before the #[fg=yellow] portion tells tmux to make the font yellow. The #(uptime | cut -d “,” -f 2-) portion tells tmux to run that cmd and output it on the right of the status bar.
Note: If you’re not familiar with shell scripting this command is very simple. It runs the uptime command and then passes it to the cut command which splits it at the commas (,). The -f 2- portion says to print out everything from the second comma onward.
Note**: By default the status bar is redrawn every 15 seconds however you can modify this by setting the status-interval command.
Misc tips & tricks
Using shell scripting to setup your tmux enviroment
Tmux allows you to easily run commands for your different sessions through the command line without having to ever login to the session. This also allows us to make a simple bootstrap script which will setup your tmux environment and log you into it. Here is an example script I personally use:
#!/bin/sh tmux new-session -d -s hawkhost tmux new-window -t hawkhost:1 -n 'Server1' 'ssh email@example.com' tmux new-window -t hawkhost:2 -n 'Server2' 'ssh firstname.lastname@example.org' tmux new-window -t hawkhost:3 -n 'Server3' 'ssh email@example.com' tmux new-window -t hawkhost:4 -n 'Server4' 'ssh firstname.lastname@example.org' tmux new-window -t hawkhost:5 -n 'Server5' 'ssh email@example.com' tmux select-window -t hawkhost:1 tmux -2 attach-session -t hawkhost
The command new-session -d -s hawkhost creates a new tmux session, detaches it (so it doesn’t open in your current terminal) and names it hawkhost in this case.
The following set of new-window commands create five new windows and executes the command at the end. The arguments are broken down as follows: -t hawkhost :1 tells tmux to “target” the session hawkhost and the window with the id of 1. The -n ‘Server1′ ‘ssh firstname.lastname@example.org’ portion tells tmux to name the window Server1 and execute the ssh email@example.com command in it.
The last two commands are pretty straight forward. The select-window -t hawkhost:1 command tells tmux that you want the active window the session to be hawkhost and window 1. The -2 attach-session -t hawkhost tells tmux you want to attach the terminal with 256 colors and attach to the session hawkhost.
Note: This is the command that brings the tmux session to the foreground. If you recall earlier when the script created the session we specified the -d switch which prevents it from loading in your terminal.
Notify you when a window has activity
This quick snippet will have tmux notify you in the status area when a window has activity:
# Set window notifications
setw -g monitor-activity on
set -g visual-activity on
Automatic window rename
You can have tmux rename the window to the command that is currently running. This is useful when you load up something such as irssi and the window is labeled accordingly:
# Automatically set window title
setw -g automatic-rename