Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 12, 2022 03:35 am GMT

Make Your Tmux Status Line 100% Better With Bash

Did you know that you can easily use Bash to display information on the Tmux status line? Because of that the possibilities of what you can do are almost endless. You can display all sorts of useful information such as:

  • Hostname
  • IP address
  • Netmask
  • Gateway
  • Memory
  • CPU temperature
  • System load
  • Battery meter
  • VPN status
  • Time
  • Date
  • Weather report
  • Git status

In this article I will show you an easy way to add information to your Tmux status line.

Requirements

  • lm-sensors

Here is the default status line.

status-line-original.png

Here is the status line enhanced to show more information.

tmux-status-line-enhanced.png

# Debian/Ubuntu based distributions$ sudo apt install tmux# Redhat/Fedora based distributions$ sudo dnf install tmux

Check your Tmux version. Currently I am using version 3.3a.

$ tmux -V# Outputtmux 3.3a

If you do not have a .tmux.conf create one in your home directory.

$ touch .tmux.conf

I will start out with a basic .tmux.conf configuration. Edit .tmux.conf and add the following lines.

# .tmux.confbind r source-file ~/.tmux.conf \; display-message "RELOADING CONFIGURATION FILE"# STATUS LINEset -g status onset -g status-interval 1set -g status-justify centre # Careful! It is spelled "centre" not "center".set -g status-style fg=white,bg=black# Highlight the current window.setw -g window-status-current-style fg=white,bg=red,bright# LEFT STATUSset -g status-left-length 100set -g status-left-style defaultset -g status-left " "# RIGHT STATUSset -g status-right-length 100set -g status-right-style defaultset -g status-right " "

Status Line Left Side

Tmux has same built in variables that you can use to display information such as #h to display the hostname. View the man page for more information.

$ man tmux

Add #h in between the double quotes

set -g status-left "#h"

You need to reload Tmux to see the changes. To reload Tmux you can type exit then launch Tmux again or you can reload Tmux by press the CTRL+b then r.
After reloading Tmux you will see the hostname of your computer on the status line.

tmux-status-line-hostname-bw.png

Adding the IP address and netmask to Status Line

All we need to do to customize the status line is to add Bash one-liners. To add Bash commands to the command line you only need to place each command inside a hash mark and parenthesis #( <my-command> ).

My Ethernet interface is enp4s0. So I use this Bash command to get the IP address of the enp4s0 Ethernet interface.

$ ip addr show enp4s0 | awk '/inet /{printf $2}'# Output192.168.0.101/26

I can add the command to the Tmux status line by placing it in between the parenthesis like this (enp4s0 is hard coded in the configuration!).

set -g status-left "#h #(ip addr show enp4s0 | awk '/inet /{printf $2}')"

tmux-status-line-ip-netmask-bw.png

Adding the CPU Temperature to Status Line

Next I want to add the CPU temperature. To make that work you must use lm-sensors. Install lm-sensors.

# Debian/Ubuntu based distributions$ sudo apt install lm-sensors# Redhat/Fedora based distributions$ sudo dnf install lm_sensors

Enable lm-sensors with this command and follow the prompts.

$ sudo sensors-detect

Run the sensors command to see the output. From the ouput you will need to figure out which line displays information about the CPU. The line you need might be labeled Tccd1 or CPU. Add the -f argument to the sensors command to see the tempurature in Fahrenheit.

$ sensors -f# Partial Output intel-isa-0000Adapter: ISA adapterfan1:           0 RPMCPU:          +96.8F  GPU:              N/A  temp3:        +96.8F  temp4:        +32.0F  temp5:        +96.8F  temp6:        +96.8F  temp7:        +96.8F  temp8:            N/A  intel-pci-0400Adapter: PCI adaptervddgfx:      794.00 mV vddnb:       604.00 mV edge:         +95.0F  PPT:           3.16 W  BAT0-acpi-0Adapter: ACPI interfacein0:          17.27 V  [....]

Add this code #(sensors -f | awk '/CPU/{printf $2}') to the end of the left status line. This command uses awk to find the row with the text CPU and print the second column.

set -g status-left "#h #(ip addr show enp4s0 | awk '/inet /{printf $2}') #(sensors -f | awk '/CPU/{printf $2}')"

tmux-status-line-cpu-temp-bw.png

Adding Memory to the Status Line

Next, I want to view the amount of memory used, the total memory, and calculate the percentage of memory in use. To do that I use the free command.

# Amount of memory used / total amount of memory$ free -m -h | awk '/Mem/{printf $3"/"$2}'# Output3.3Gi/62Gi# Calculate percentage of memory used$ free -m | awk '/Mem/{printf "%.2f%", 100*$3/$2}'# Output2.1%

Careful here! To get the memory command to display properly in the Tmux status line requires that certain characters be escaped.
Here I am using the backward slash \ to escape the double quote \", the period \., the percent sign \%, and the forward slash \/.

Before escaping characters:

#(free -m -h | awk '/Mem/{printf $3"/"$2}') #(free -m | awk '/Mem{printf "%.2f%", 100*$2/$3}')"

After escaping characters:

#(free -m -h | awk '/Mem/{printf $3\"\/\"$2}') #(free -m | awk '/Mem{printf \"\%\.2f\%\", 100*$2/$3}')"

The status left should now look like this.

set -g status-left "#h #(ip addr show enp4s0 | awk '/inet /{printf $2}') #(sensors | awk '/CPU/{printf $2}') #(free -m -h | awk '/Mem/{printf $3\"\/\"$2}') #(free -m | awk '/Mem{printf \"\%\.2f\%\", 100*$2/$3}')"

tmux-status-line-memory-bw.png

Adding the Battery Level to Status Line

I want to see the battery level and see if it is charging on not. To do that I can check the /sys file system for the battery information.

If your terminal supports unicode characters you can add an unicode characters to the status bar.

I will test to see if the power cable is plugged in and show a lighting bolt icon if it is. I will show the percentage of battery remaining next to the lightning bolt.

If $(cat /sys/class/power_supply/AC/online) is equal to 1 the lightning bolt will display.

Add these lines to the end of the left status line.

#([ $(cat /sys/class/power_supply/AC/online) == 1 ] && printf %s'') #( $(cat /sys/class/power_supply/BAT0/capacity)\% )

The left status line should look something like this.

set -g status-left "#h #(ip addr show enp4s0 | awk '/inet /{printf $2}') #(sensors | awk '/CPU/{printf $2}') #(free -m -h | awk '/Mem/{printf \$3\"\/\"\$2\}') #(free -m | awk '/Mem{printf 100*$2/$3" %"}') #([ $(cat /sys/class/power_supply/AC/online) == 1 ] && printf %s'') #(cat /sys/class/power_supply/BAT0/capacity)\%"

tmux-status-line-battery-bw.png

Adding the VPN to Status Line

I want to check if the VPN is up and see the IP address assigned to it. Depending on the VPN you use you might have to check the tun0 or ppp0 interface.

ip addr show tun0 | awk '/tun0/{printf $2}'# Output10.20.3.45

Add this to the end of the left status line.

#(ip addr show tun0 | awk '/tun0/{printf \"VPN: \"$2}')

My status line now looks like this.

set -g status-left "#h #(ip addr show enp4s0 | awk '/inet /{printf $2}') #(sensors | awk '/CPU/{printf $2}') #(free -m -h | awk '/Mem/{printf \$3\"\/\"\$2\}') #(free -m | awk '/Mem{printf 100*$2/$3" %"}') #([ $(cat /sys/class/power_supply/AC/online) == 1 ] && printf %s'') #(cat /sys/class/power_supply/BAT0/capacity)\% #(ip a | awk '/tun0/{printf \"VPN: \" $2}')"

tmux-status-line-enhanced-bw.png

Status Bar Right Side

Now lets configure the right side.

I will get the load average using the uptime command.

$ uptime# Output02:39:38 up 12:18,  1 user,  load average: 0.20, 0.11, 0.10

The number of columns in the output can change so depending on the time. To get around that problem I use $(NF) to get the last column and tr to remove the colon characters.

Add this to the right status line option with escaped characters in between the double quotes.

set -g status-right "#(uptime | awk '{printf \$(NF-2)\" \"\$(NF-1)\" \"\$(NF)}' | tr -d ',')"

The last thing I will add is the date, time, and timezone using variables to status-right. Tmux uses strftime formatting to display variables. Given that, I can view the date and time using a custom format. Example: %F represents YYYY-mm-dd %T represents HH:MM:SS . See the man page for more information man strftime.

Add that to the end of the right status line.

set -g status-right "#(uptime | awk '{printf \$(NF-2)\" \"\$(NF-1)\" \"\$(NF)}' | tr -d ',') %F %T %Z"

Adding a Bash Script

If you needed to configure more complicated code than what could fit on one line you can make a separate Bash file and add it like this.

set -g status-right "#( example.sh )"
$ cat example.sh#!/bin/bash# example.sh# Get the IP address dynamicallyfunction get_ip_address() {    for iface in /sys/class/net/*/operstate; do        if [ "$(echo $iface | awk -F'/' '{print $5}')" != "lo" ]; then            if [ "$(cat $iface)" == "up" ] ; then                interface=$(echo $iface | awk -F'/' '{print $5}')                printf "%s " "$(ip addr show $interface | awk '/inet /{print $2}')"            fi        fi    done }get_ip_address

Adding Color

Run this code in your terminal to see what colors you have available.

#!/bin/bashfor num in {0..255}; do      printf "%s\033[38;5;${num}mcolour${num}\033[0m ";     if [ $(expr $((num+1)) % 8) -eq 0 ]; then        printf "
" fidone

tmux-colour-chart.png

Now you can add color to your status line. Add foreground colors by setting a #[fg=<colourN>] or #[bg=<colourN>] variable to the color of your choice.
fg= is for the forground color bg= is to set the background color in between square brackets. Use #[default] to back to the default colors.

set -g status-left "#[fg=colour220] #h #[fg=colour47] #(ip addr show enp4s0 | awk '/inet /{printf $2}') #[colour27] #(sensors -f | awk '/CPU/{printf $2}') #[default]"

tmux-status-line-color.png

The status lines can get quite long. If you do not like long lines use the backslash to break it up.

set -g status-left "#[fg=colour220]#h\#[fg=colour196] #(ip addr show enp4s0 | awk '/inet /{printf $2}')\#[fg=colour39] #(sensors | awk '/CPU/{printf $2}')\#[fg=colour40] #(free -m -h | awk '/Mem/{printf \$3\"\/\"\$2\}')\#[fg=colour128] #(free -m | awk '/Mem{printf 100*$2/$3" %"}')\#[fg=colour202] #([ $(cat /sys/class/power_supply/AC/online) == 1 ] && printf %s'') #(cat /sys/class/power_supply/BAT0/capacity)\%\#[fg=colour7] #(ip a | awk '/tun0/{printf \"VPN: \" $2}')\#[default]"

Complete Status Line Configuration

# .tmux.confbind r source-file ~/.tmux.conf \; display-message "RELOADING CONFIGURATION FILE"# STATUS LINEset -g status onset -g status-interval 1set -g status-justify centre # Careful! It is spelled "centre" not "center".set -g status-style fg=white,bg=black# Highlight the current window.setw -g window-status-current-style fg=white,bg=red,bright# LEFT STATUSset -g status-left-length 100set -g status-left-style defaultset -g status-left "#[fg=colour220]#h\#[fg=colour196] #(ip addr show enp4s0 | awk '/inet /{printf $2}')\#[fg=colour39] #(sensors | awk '/CPU/{printf $2}')\#[fg=colour40] #(free -m -h | awk '/Mem/{printf \$3\"\/\"\$2\}')\#[fg=colour128] #(free -m | awk '/Mem{printf 100*$2/$3" %"}')\#[fg=colour202] #([ $(cat /sys/class/power_supply/AC/online) == 1 ] && printf %s'') #(cat /sys/class/power_supply/BAT0/capacity)\%\#[fg=colour7] #(ip a | awk '/tun0/{printf \"VPN: \" $2}')\#[default]"# RIGHT STATUSset -g status-right-length 100set -g status-right-style defaultset -g status-right "#[fg=colour39] #(uptime | awk '{printf \$(NF-2)\" \"\$(NF-1)\" \"\$(NF)}' | tr -d ',')\#[fg=colour40] %F\#[fg=colour128] %T\#[fg=colour202] %Z\#[default]"

Conclusion

You can now easily have a Bash display useful information on the Tmux status line. I hope you learned something new and enjoyed reading this article.

Follow me on Dev.to and Github.

Feel free to leave comments, questions, and suggestions.


Original Link: https://dev.to/brandonwallace/make-your-tmux-status-line-100-better-with-bash-mgf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To