paint-brush
Customizing Tmuxby@sambernheim
44,205 reads
44,205 reads

Customizing Tmux

by Sam BernheimJanuary 26th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I assume you already have a .tmux.conf file set up that is already being used. If not first look up setting up .tmux.conf or something similar.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Customizing Tmux
Sam Bernheim HackerNoon profile picture

I assume you already have a .tmux.conf file set up that is already being used. If not first look up setting up .tmux.conf or something similar.

Step 1: How to Change the Status Line

set -g status-left <options>

set -g status-right <options>

Replace <options> with a string of all the options you want. Background color can be set using #[bg=colour*] where * is some number 0–255 for terminals with 256 color support. To change the foreground color (text color) it’s just #[fg=colour*] (Again replacing * with a number between 0 and 255).

To view all 255 colors at once see the comment of this stack overflow answer

One of the first things I wanted to do was to add the date and time in the status line. Simple enough right. Here is a complete list of options https://linux.die.net/man/3/strftime or for a shorter more readable list see part of my [.tmux.conf](https://github.com/sbernheim4/dotfiles/blob/master/.tmux.conf#L59.)

Adding %H:%M in the <options> string will show the current time in a 24 hour format without seconds (something like 15:38).

Step 2: Tmux Specific Values

Tmux has its own set of values that stand for different things. For example #H refers to the hostname of the computer you are currently on and #S to the index of the current session. For the complete list of options go here and search for this phrase “Character pair Replaced with”

— UPDATE 4/20/18 —

So I recently learned how to show or hide text in the status line given the current state (if the prefix has been pressed or if a window is zoomed etc). The official man page of tmux has a list of attributes which keep track of the state of the pane/window/session. Visit the page and search for “The following variables are available, where appropriate” and you will see a long list of options. How can these be used you ask, well for knowing if the prefix has been pressed or not I change the color of part of my status line with the following code.

#{?client_prefix,#[bg=colour2],}

If the prefix is active (as in I just pressed it), then it inserts the text after the first comma and otherwise inserts the text after the second comma which is nothing in this case. Here I’m inserting text to change the background color but any text can be inserted including… ICONS. For displaying an icon if the current window is zoomed in (which can be done with prefix + z) see the code below.

#{?window_zoomed_flag, ,🔍}

This will add a magnifying glass in the status line wherever this is placed in the line. (For icons, you might have to play around with adding additional spaces before and after the icon. I add an extra space here both before and after to keep it from looking cramped).

— END UPDATE —

Step 3: Using Bash Commands to Display Cool Stuff

The last key step is being able to display cool information using bash commands. This can include things like battery percentage, current song playing etc. To do this you first have to find a bash script or command that gives you the output you want. For example, I display the current battery percentage using the following command:

pmset -g batt | grep [0-9][0-9]% | awk ‘NR==1{print$3}’ | cut -c 1–3

Note: This only works for Macs since pmset is a mac specific terminal command.

I have a file stored on my computer called battery.sh. The file is two lines long. Line 1 is the bash shebang (#!/bin/bash) and line 2 is the command above. To show the battery in the status line, in my .tmux.conf in <options> I have

#(path/to/battery.sh)

(Pro Tip: adding an emoji like: ♥ before the # will display it as ♥ 64% in the status line which looks cooler than just 64%)

Step 4: Plugins

Alright so these aren’t really plugins so much as they are scripts that other people have already written. Check out https://github.com/jdxcode/tmux-spotify-info for getting the current playing Spotify song, this again is mac only because the developer wrote this script in apple script. But the point is you can find scripts that other people already wrote for your environment and easily add them to your status line.

Step 5: Customizing the Windows

Hopefully you already know the difference between sessions, windows, and panes. if not, quickly read up on that.

Point is, beyond the left and right side of the status line you can also customize how the windows are displayed in the status with:

set -g window-status-format <options>

where <options> is the same configuration as it was way above. You also can customize the display for the current window you are on with:

set -g window-status-current-format <options>

See this site again and search “Character pair Replaced with” to view some specially reserved Tmux commands for windows. You can also then customize the separators that are used to show the difference between windows. Customizing the separators may require a patched font to display certain characters. A quick google search of patched fonts should get you some good results. For cool unicode character separators (still might require a patched font even though they’re unicode) check out this site.

hint: try searching stuff like left triangle

Thanks for reading. View my own .tmux.conf