Display CPU usage in tmux by installing the tmux-cpu
plugin or by adding a custom script to your status bar configuration. With the plugin, add set -g @plugin 'tmux-plugins/tmux-cpu'
to your ~/.tmux.conf
file and include #{cpu_percentage}
in your status line format. For a custom solution, use a shell script that outputs CPU data and call it from your tmux configuration.
set -g status-right "CPU: #{cpu_percentage} | %H:%M"
Monitoring CPU usage directly in your tmux status bar can be helpful for system administrators, developers, and power users who need to keep an eye on system resources while working.
The easiest way to display CPU usage is with the tmux-cpu plugin:
# Step 1: Install TPM if you haven't already git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm # Step 2: Add the plugin to your ~/.tmux.conf set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-cpu' # Step 3: Add CPU information to your status line set -g status-right "CPU: #{cpu_percentage} | %H:%M" # Step 4: Initialize TMUX plugin manager (keep this at the bottom) run '~/.tmux/plugins/tpm/tpm'
After adding these lines, reload your tmux configuration with Ctrl+b : source-file ~/.tmux.conf
and install the plugin with Ctrl+b I.
The tmux-cpu plugin provides several format strings:
#{cpu_percentage}
- Shows CPU usage percentage#{cpu_icon}
- Shows a CPU icon that changes color based on usage#{cpu_bg_color}
- Changes background color based on CPU load#{ram_percentage}
- Shows RAM usage percentage#{ram_icon}
- Shows a RAM icon that changes color#{gpu_percentage}
- Shows GPU usage percentage (if supported)You can combine these in your status line configuration:
set -g status-right "#{cpu_icon} #{cpu_percentage} | #{ram_icon} #{ram_percentage} | %H:%M"
If you prefer not to use a plugin, you can create a custom solution using shell commands and tmux's ability to run external commands in the status line:
# Create a script to get CPU usage (save as ~/bin/cpu_usage.sh) #!/bin/bash # Get CPU usage as a percentage cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}') echo "$cpu_usage" # Make it executable chmod +x ~/bin/cpu_usage.sh # Then in your ~/.tmux.conf set -g status-interval 5 # Update every 5 seconds set -g status-right "#[fg=green]CPU: #(~/bin/cpu_usage.sh) | %H:%M"
This approach runs the script every 5 seconds to update the CPU usage in your status bar. You may need to adjust the CPU usage calculation based on your operating system.
# For macOS (save as ~/bin/cpu_usage_mac.sh) #!/bin/bash top -l 1 | grep -E "^CPU" | awk '{print $3+$5"%"}' # For Linux (save as ~/bin/cpu_usage_linux.sh) #!/bin/bash grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
Add color-coding to your CPU usage display for better visual feedback:
# Add to your ~/.tmux.conf set -g status-right "\ #[fg=green,bg=default]#($HOME/bin/cpu_usage.sh < 30 && echo '#[fg=green]' || echo '#[fg=yellow]')\ #($HOME/bin/cpu_usage.sh > 70 && echo '#[fg=red]' || echo '')\ CPU: #($HOME/bin/cpu_usage.sh) #[fg=default]| %H:%M"
This changes the color to yellow when CPU usage is above 30% and red when it's above 70%.
For comprehensive system monitoring, consider combining CPU usage with other metrics:
# Full system monitoring in status bar set -g status-right "\ CPU: #{cpu_percentage} \ RAM: #{ram_percentage} \ | #[fg=cyan]#(df -h | grep '/$' | awk '{print $5}') \ | #[fg=green]↑#(uptime | awk '{print $3,$4}' | sed 's/,//') \ | #[fg=white]%a %d-%b %H:%M"