在 tmux 环境中使用 tmux-256color

1,010 阅读1分钟

本文首发于 at7h 的个人博客

在 macOS 中,我们 iTerm2 中的 terminal color 一般都设置为 xterm-256color,这没毛病,但问题是它不能在 tmux 环境中使用: $TERM should be "screen-256color" or "tmux-256color" in tmux. Colors might look wrong.

为此,我们可以在 zshrc 中加上这样一段配置:

if [[ $TMUX != "" ]] then
    export TERM="tmux-256color"
else
    export TERM="xterm-256color"
fi

而在 tmux 环境中一般默认使用系统自带的 screen-256color,这在大多数情况是够用的,但是它不支持任何斜体字体样式,所以在 Vim 中类似代码高亮这种就会很有问题。

为此,我们可以在 tmux.conf 中加上设置:

set -g default-terminal "tmux-256color"
set-option -a terminal-overrides ",*256col*:RGB"

新建一个 session 测试一下:

tnew test # tmux new -s test

发现报错: can't find terminal definition for tmux-256color

原因是系统默认是没有 tmux-256color 的,需要我们手动安装,加上相应的 terminfo 就可以了。


tmux-256color 详细安装教程:Installing tmux-256color for macOS

感兴趣的同学可以参考我的配置,欢迎大家评论留言,与我交流。