能否修改tmux window-status-format中的默认标记及窗口编号格式?
自定义tmux窗口状态格式:替换标记符号与罗马数字索引
当然可以修改这些默认值!tmux的窗口状态格式灵活性拉满,下面分两部分解决你的需求:
1. 替换当前/上次窗口的标记符号(* 和 -)
默认的#F会根据窗口状态显示*(当前窗口)、-(上次使用的窗口)或空字符串(其他窗口)。如果你想替换这些符号,直接用tmux的条件表达式自定义会更直观:
在你的~/.tmux.conf里添加:
# 非当前窗口的格式:标记(仅上次窗口显示)+ 索引 + 名称 set-option -g window-status-format "#{?window_last,○,} #I:#W" # 当前窗口的格式:自定义标记 + 索引 + 名称(可搭配高亮) set-option -g window-status-current-format "● #I:#W"
这里用●代替默认的*,○代替默认的-,你可以换成任何喜欢的符号(比如🔵、🟢,甚至 emoji)。
如果想保留#F的逻辑但替换符号,也可以用字符串替换的方式:
set-option -g window-status-format "#{replace:#{replace:#F,*,-},-,○} #I:#W" set-option -g window-status-current-format "#{replace:#F,*,-} #I:#W"
不过这种方式不如条件表达式清晰,更推荐第一种方案。
2. 将阿拉伯数字索引(#I)转为罗马数字
tmux本身没有内置罗马数字转换,但可以通过调用外部命令实现。你可以用一行Python/awk命令转换索引,嵌入到格式字符串中:
方案1:直接用Python一行命令
# 非当前窗口:标记 + 罗马数字索引 + 名称 set-option -g window-status-format "#{?window_last,○,} #{run-shell "python3 -c \"def to_roman(n): r='' ; for v,s in [(1000,'M'),(900,'CM'),(500,'D'),(400,'CD'),(100,'C'),(90,'XC'),(50,'L'),(40,'XL'),(10,'X'),(9,'IX'),(5,'V'),(4,'IV'),(1,'I')]: while n>=v: r+=s; n-=v; return r; print(to_roman(int('#{window_index}')))\""}:#W" # 当前窗口:自定义标记 + 罗马数字索引 + 名称 set-option -g window-status-current-format "● #{run-shell "python3 -c \"def to_roman(n): r='' ; for v,s in [(1000,'M'),(900,'CM'),(500,'D'),(400,'CD'),(100,'C'),(90,'XC'),(50,'L'),(40,'XL'),(10,'X'),(9,'IX'),(5,'V'),(4,'IV'),(1,'I')]: while n>=v: r+=s; n-=v; return r; print(to_roman(int('#{window_index}')))\""}:#W"
方案2:用独立脚本简化配置
如果嫌上面的命令太长,可以把罗马数字转换写成单独的脚本(比如~/.tmux/scripts/roman.sh):
#!/bin/bash to_roman() { local n=$1 local roman="" declare -a val=(1000 900 500 400 100 90 50 40 10 9 5 4 1) declare -a sym=("M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I") for ((i=0; i<${#val[@]}; i++)); do while (( n >= ${val[$i]} )); do roman+="${sym[$i]}" ((n -= ${val[$i]})) done done echo "$roman" } to_roman "$1"
给脚本加执行权限:chmod +x ~/.tmux/scripts/roman.sh,然后在tmux配置里调用:
set-option -g window-status-format "#{?window_last,○,} #{run-shell "~/.tmux/scripts/roman.sh #{window_index}"}:#W" set-option -g window-status-current-format "● #{run-shell "~/.tmux/scripts/roman.sh #{window_index}"}:#W"
生效方式
修改完~/.tmux.conf后,在tmux会话里按Ctrl+b :,输入source-file ~/.tmux.conf回车,就能看到效果了。
内容的提问来源于stack exchange,提问作者ma4stro
相关产品推荐
相关产品推荐

