Linux下tmux配置
1. 在~/.bashrc中设置tmux的alias
alias ltmux=~/.bin/tmux.sh
2. ~/.bin/tmux.sh脚本
此脚本能判断如是在tty下,则启用~/.ttytmux.conf配置文件,否则启用默认配置文件;并当判断会话是否存在,如不存在则新建,否则显示此会话。
#!/bin/bash
if [ "$TERM" == "linux" ]
then
if ! tmux has-session -t tty-host; then
tmux -f ~/.ttytmux.conf new-session -s tty-host -n tty-host-console
else
tmux attach -t tty-host
fi
else
if ! tmux has-session -t host; then
tmux new-session -s host -n host-console
else
tmux attach -t host
fi
fi
3. ~/.ttytmux.conf配置文件
# my .ttytmux.conf file # tmux in tty mode # Setting the prefix from C-b to C-a set -g prefix C-a # Free the original Ctrl-b prefix keybinding unbind C-b # Setting the delay between prefix and command set -sg escape-time 1 # Ensure that we can send Ctrl-A to other apps bind C-a send-prefix # Set the base index for windows to 1 instead of 0 set -g base-index 1 # Set the base index for panes to 1 instead of 0 setw -g pane-base-index 1 # Reload the file with Prefix r bind r source-file ~/.tmux.conf \; display "configure Reloaded!" bind g source-file ~/.ttytmux.conf \; display "white configure Reloaded!" # Splitting panes bind | split-window -h bind - split-window -v # Moving between panes bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R # Quick windows selection bind -r C-h select-window -t :- bind -r C-l select-window -t :+ # Pane resizing bind -r H resize-pane -L 10 bind -r J resize-pane -D 10 bind -r K resize-pane -U 10 bind -r L resize-pane -R 10 # Mouse support - set to on if you want to use the mouse #setw -g mode-mouse off #set -g mouse-select-pane off #set -g mouse-resize-pane off #set -g mouse-select-window off # Set the default terminal mode to 256color mode set -g default-terminal "screen-256color" # Enable activity alerts setw -g monitor-activity on set -g visual-activity on # Set the status line's colors set -g status-fg black set -g status-bg white # Set the color of the window list setw -g window-status-fg cyan setw -g window-status-bg default setw -g window-status-attr dim # Set colors for the active window setw -g window-status-current-fg white setw -g window-status-current-bg black setw -g window-status-current-attr bright # Panes colors set -g pane-border-fg green set -g pane-border-bg black set -g pane-active-border-fg white set -g pane-active-border-bg yellow # Command / message line set -g message-fg white set -g message-bg black set -g message-attr bright # Status line left side set -g status-left-length 40 set -g status-left "#[fg=black]Session:#S #[fg=black]Windows:#I #[fg=black]Panes:#P" # set -g status-utf8 on # Status line right side # 15% | 28 Nov 18:15 set -g status-right "#(~/.bin/ttybattery -t -g 75) | #[fg=black]%B %d %A %R" # Update the status bar every sixty seconds set -g status-interval 60 # Center the window list set -g status-justify centre # Enable vi keys setw -g mode-keys vi # Open panes in the same directory using the tmux-panes script unbind v unbind n bind v send-keys " ~/tmux-panes -h" C-m bind n send-keys " ~/tmux-panes -v" C-m # Maximize and restore a pane unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp unbind Down bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp # Log output to a text file on demand bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log" # zoom pane <-> window # http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/examples/tmux-zoom.sh bind ^z run "tmux-zoom"
4. ~/.tmux.conf配置文件
# my .tmux.conf file # Setting the prefix from C-b to C-a set -g prefix C-a # Free the original Ctrl-b prefix keybinding unbind C-b # Setting the delay between prefix and command set -sg escape-time 1 # Ensure that we can send Ctrl-A to other apps bind C-a send-prefix # Set the base index for windows to 1 instead of 0 set -g base-index 1 # Set the base index for panes to 1 instead of 0 setw -g pane-base-index 1 # Reload the file with Prefix r bind r source-file ~/.tmux.conf \; display "configure Reloaded!" bind g source-file ~/.ttytmux.conf \; display "white configure Reloaded!" # Splitting panes bind | split-window -h bind - split-window -v # Moving between panes bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R # Quick windows selection bind -r C-h select-window -t :- bind -r C-l select-window -t :+ # Pane resizing bind -r H resize-pane -L 10 bind -r J resize-pane -D 10 bind -r K resize-pane -U 10 bind -r L resize-pane -R 10 # Mouse support - set to on if you want to use the mouse #setw -g mode-mouse off #set -g mouse-select-pane off #set -g mouse-resize-pane off #set -g mouse-select-window off # Set the default terminal mode to 256color mode set -g default-terminal "screen-256color" # Enable activity alerts setw -g monitor-activity on set -g visual-activity on # Set the status line's colors set -g status-fg white set -g status-bg black # Set the color of the window list setw -g window-status-fg cyan setw -g window-status-bg default setw -g window-status-attr dim # Set colors for the active window setw -g window-status-current-fg white # setw -g window-status-current-bg red setw -g window-status-current-bg black setw -g window-status-current-attr bright # Panes colors set -g pane-border-fg green set -g pane-border-bg black set -g pane-active-border-fg white set -g pane-active-border-bg yellow # Command / message line set -g message-fg white set -g message-bg black set -g message-attr bright # Status line left side set -g status-left-length 40 set -g status-left "#[fg=green]Session:#S #[fg=blue]Windows:#I #[fg=yellow]Panes:#P" # set -g status-utf8 on # Status line right side # 15% | 28 Nov 18:15 set -g status-right "#(~/.bin/battery -t) | #[fg=cyan]%B %d %A %R" # Update the status bar every sixty seconds set -g status-interval 60 # Center the window list set -g status-justify centre # Enable vi keys setw -g mode-keys vi # Open panes in the same directory using the tmux-panes script unbind v unbind n bind v send-keys " ~/tmux-panes -h" C-m bind n send-keys " ~/tmux-panes -v" C-m # Maximize and restore a pane unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp unbind Down bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp # Log output to a text file on demand bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log" # zoom pane <-> window # http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/examples/tmux-zoom.sh bind ^z run "tmux-zoom"
5. ~/.bin/battery配置文件
#!/bin/bash
usage() {
cat <<EOF
battery usage:
general:
-h, --help print this message
-t output tmux status bar format
-z output zsh prompt format
-e don't output the emoji
-a output ascii instead of spark
-b battery path default: /sys/class/power_supply/BAT0
-p use pmset (more accurate)
colors: tmux zsh
-g <color> good battery level default: 1;32 | green | 64
-m <color> middle battery level default: 1;33 | yellow | 136
-w <color> warn battery level default: 0;31 | red | 160
EOF
}
if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]; then
usage
exit 0
fi
# For default behavior
setDefaults() {
pmset_on=0
output_tmux=0
output_zsh=0
ascii=0
ascii_bar='=========='
emoji=1
good_color="1;32"
middle_color="1;33"
warn_color="0;31"
connected=0
battery_path=/sys/class/power_supply/BAT0
}
setDefaults
# Determine battery charge state
battery_charge() {
case $(uname -s) in
"Darwin")
if ((pmset_on)) && command -v pmset &>/dev/null; then
if [ "$(pmset -g batt | grep -o 'AC Power')" ]; then
BATT_CONNECTED=1
else
BATT_CONNECTED=0
fi
BATT_PCT=$(pmset -g batt | grep -o '[0-9]*%' | tr -d %)
else
while read key value; do
case $key in
"MaxCapacity")
maxcap=$value
;;
"CurrentCapacity")
curcap=$value
;;
"ExternalConnected")
if [ $value == "No" ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
;;
esac
if [[ -n "$maxcap" && -n $curcap ]]; then
BATT_PCT=$(( 100 * curcap / maxcap))
fi
done < <(ioreg -n AppleSmartBattery -r | grep -o '"[^"]*" = [^ ]*' | sed -e 's/= //g' -e 's/"//g' | sort)
fi
;;
"Linux")
case $(cat /etc/*-release) in
*"Arch Linux"*|*"Ubuntu"*)
battery_state=$(cat $battery_path/energy_now)
battery_full=$battery_path/energy_full
battery_current=$battery_path/energy_now
;;
*)
battery_state=$(cat $battery_path/status)
battery_full=$battery_path/charge_full
battery_current=$battery_path/charge_now
;;
esac
if [ $battery_state == 'Discharging' ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
now=$(cat $battery_current)
full=$(cat $battery_full)
BATT_PCT=$((100 * $now / $full))
;;
esac
}
# Apply the correct color to the battery status prompt
apply_colors() {
# Green
if [[ $BATT_PCT -ge 75 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$good_color]"
elif ((output_zsh)); then
COLOR="%F{$good_color}"
else
COLOR=$good_color
fi
# Yellow
elif [[ $BATT_PCT -ge 25 ]] && [[ $BATT_PCT -lt 75 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$middle_color]"
elif ((output_zsh)); then
COLOR="%F{$middle_color}"
else
COLOR=$middle_color
fi
# Red
elif [[ $BATT_PCT -lt 25 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$warn_color]"
elif ((output_zsh)); then
COLOR="%F{$warn_color}"
else
COLOR=$warn_color
fi
fi
}
# Print the battery status
print_status() {
if ((emoji)) && ((BATT_CONNECTED)); then
GRAPH="⚡"
else
if command -v spark &>/dev/null; then
sparks=$(spark 0 ${BATT_PCT} 100)
GRAPH=${sparks:1:1}
else
ascii=1
fi
fi
if ((ascii)); then
barlength=${#ascii_bar}
# Battery percentage rounded to the lenght of ascii_bar
rounded_n=$(( $barlength * $BATT_PCT / 100 + 1))
# Creates the bar
GRAPH=$(printf "[%-${barlength}s]" "${ascii_bar:0:rounded_n}")
fi
if ((output_tmux)); then
printf "%s%s %s%s" "$COLOR" "[$BATT_PCT%]" "$GRAPH" "#[default]"
elif ((output_zsh)); then
printf "%%B%s%s %s" "$COLOR" "[$BATT_PCT%%]" "$GRAPH"
else
printf "\e[0;%sm%s %s \e[m\n" "$COLOR" "[$BATT_PCT%]" "$GRAPH"
fi
}
# Read args
while getopts ":g:m:w:tzeab:p" opt; do
case $opt in
g)
good_color=$OPTARG
;;
m)
middle_color=$OPTARG
;;
w)
warn_color=$OPTARG
;;
t)
output_tmux=1
good_color="green"
middle_color="yellow"
warn_color="red"
;;
z)
output_zsh=1
good_color="64"
middle_color="136"
warn_color="160"
;;
e)
emoji=0
;;
a)
ascii=1
;;
p)
pmset_on=1
;;
b)
if [ -d $OPTARG ]; then
battery_path=$OPTARG
else
>&2 echo "Battery not found, trying to use default path..."
if [ ! -d $battery_path ]; then
>&2 echo "Default battery path is also unreachable"
exit 1
fi
fi
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done
battery_charge
apply_colors
print_status
6. ~/.bin/ttybattery配置文件
#!/bin/bash
usage() {
cat <<EOF
battery usage:
general:
-h, --help print this message
-t output tmux status bar format
-z output zsh prompt format
-e don't output the emoji
-a output ascii instead of spark
-b battery path default: /sys/class/power_supply/BAT0
-p use pmset (more accurate)
colors: tmux zsh
-g <color> good battery level default: 1;32 | green | 64
-m <color> middle battery level default: 1;33 | yellow | 136
-w <color> warn battery level default: 0;31 | red | 160
EOF
}
if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]; then
usage
exit 0
fi
# For default behavior
setDefaults() {
pmset_on=0
output_tmux=0
output_zsh=0
ascii=0
ascii_bar='=========='
emoji=1
good_color="1;32"
middle_color="1;33"
warn_color="0;31"
connected=0
battery_path=/sys/class/power_supply/BAT0
}
setDefaults
# Determine battery charge state
battery_charge() {
case $(uname -s) in
"Darwin")
if ((pmset_on)) && command -v pmset &>/dev/null; then
if [ "$(pmset -g batt | grep -o 'AC Power')" ]; then
BATT_CONNECTED=1
else
BATT_CONNECTED=0
fi
BATT_PCT=$(pmset -g batt | grep -o '[0-9]*%' | tr -d %)
else
while read key value; do
case $key in
"MaxCapacity")
maxcap=$value
;;
"CurrentCapacity")
curcap=$value
;;
"ExternalConnected")
if [ $value == "No" ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
;;
esac
if [[ -n "$maxcap" && -n $curcap ]]; then
BATT_PCT=$(( 100 * curcap / maxcap))
fi
done < <(ioreg -n AppleSmartBattery -r | grep -o '"[^"]*" = [^ ]*' | sed -e 's/= //g' -e 's/"//g' | sort)
fi
;;
"Linux")
case $(cat /etc/*-release) in
*"Arch Linux"*|*"Ubuntu"*)
battery_state=$(cat $battery_path/energy_now)
battery_full=$battery_path/energy_full
battery_current=$battery_path/energy_now
;;
*)
battery_state=$(cat $battery_path/status)
battery_full=$battery_path/charge_full
battery_current=$battery_path/charge_now
;;
esac
if [ $battery_state == 'Discharging' ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
now=$(cat $battery_current)
full=$(cat $battery_full)
BATT_PCT=$((100 * $now / $full))
;;
esac
}
# Apply the correct color to the battery status prompt
apply_colors() {
# Green
if [[ $BATT_PCT -ge 75 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$good_color]"
elif ((output_zsh)); then
COLOR="%F{$good_color}"
else
COLOR=$good_color
fi
# Yellow
elif [[ $BATT_PCT -ge 25 ]] && [[ $BATT_PCT -lt 75 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$middle_color]"
elif ((output_zsh)); then
COLOR="%F{$middle_color}"
else
COLOR=$middle_color
fi
# Red
elif [[ $BATT_PCT -lt 25 ]]; then
if ((output_tmux)); then
COLOR="#[fg=$warn_color]"
elif ((output_zsh)); then
COLOR="%F{$warn_color}"
else
COLOR=$warn_color
fi
fi
}
# Print the battery status
print_status() {
if ((emoji)) && ((BATT_CONNECTED)); then
GRAPH="⚡"
else
if command -v spark &>/dev/null; then
sparks=$(spark 0 ${BATT_PCT} 100)
GRAPH=${sparks:1:1}
else
ascii=1
fi
fi
if ((ascii)); then
barlength=${#ascii_bar}
# Battery percentage rounded to the lenght of ascii_bar
rounded_n=$(( $barlength * $BATT_PCT / 100 + 1))
# Creates the bar
GRAPH=$(printf "[%-${barlength}s]" "${ascii_bar:0:rounded_n}")
fi
if ((output_tmux)); then
printf "%s%s %s%s" "$COLOR" "[$BATT_PCT%]" "$GRAPH" "#[default]"
elif ((output_zsh)); then
printf "%%B%s%s %s" "$COLOR" "[$BATT_PCT%%]" "$GRAPH"
else
printf "\e[0;%sm%s %s \e[m\n" "$COLOR" "[$BATT_PCT%]" "$GRAPH"
fi
}
# Read args
while getopts ":g:m:w:tzeab:p" opt; do
case $opt in
g)
good_color=$OPTARG
;;
m)
middle_color=$OPTARG
;;
w)
warn_color=$OPTARG
;;
t)
output_tmux=1
good_color="green"
middle_color="yellow"
warn_color="red"
;;
z)
output_zsh=1
good_color="64"
middle_color="136"
warn_color="160"
;;
e)
emoji=0
;;
a)
ascii=1
;;
p)
pmset_on=1
;;
b)
if [ -d $OPTARG ]; then
battery_path=$OPTARG
else
>&2 echo "Battery not found, trying to use default path..."
if [ ! -d $battery_path ]; then
>&2 echo "Default battery path is also unreachable"
exit 1
fi
fi
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done
battery_charge
apply_colors
print_status
浙公网安备 33010602011771号