
7

改变终端下的光标颜色,包括 screen 和 tmux!
source link: https://blog.lilydjwg.me/2011/8/24/change-cursor-color-in-terminal-including-screen-and-tmux.29019.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

改变终端下的光标颜色,包括 screen 和 tmux!
本文来自依云's Blog,转载请注明。
曾经在Ubuntu中文论坛里看到一个改变光标颜色的方法,用光标颜色来指示是在 Vim 的普通模式还是插入模式下(因为 gnome-terminal 不支持使用转义序列改变光标形状)。Vim Wiki 上的 tip。
if
&
term
=~
"xterm\|rxvt"
silent
!echo -ne
"\e]12;HotPink\007"
let
&t_SI=
"\e]12;RoyalBlue1\007"
let
&t_EI=
"\e]12;HotPink\007"
autocmd
VimLeave
* :!echo -ne
"\e]12;green\007"
endif
可惜它不适用于当时我正在使用的 screen。现在我改用 tmux 了,偶然改变TERM
变量测试的时候,发现光标颜色竟然改变了——虽然还附带一些“不良反应”。我想到:一定有办法来正确地改变光标颜色的!
于是求助于 Google,很快找到了这个,有了用于 screen 的转义序列。不过依旧不适用于 tmux。把“tmux”也加到关键词里再搜,终于找到了这个。根据这个帖子,screen 和 tmux 比 xterm 多出来的那些字符序列是告诉 screen 或者 tmux 把其中的字符序列直接发送到终端模拟器处理。
于是,我的 vimrc 又可以更新了:
let
color_normal =
'HotPink'
let
color_insert =
'RoyalBlue1'
let
color_exit =
'green'
if
&
term
=~
'xterm\|rxvt'
exe
'silent !echo -ne "\e]12;"'
. shellescape(color_normal, 1) .
'"\007"'
let
&t_SI=
"\e]12;"
. color_insert .
"\007"
let
&t_EI=
"\e]12;"
. color_normal .
"\007"
exe
'autocmd VimLeave * :!echo -ne "\e]12;"'
. shellescape(color_exit, 1) .
'"\007"'
elseif
&
term
=~
"screen"
if
!exists(
'$SUDO_UID'
)
if
exists(
'$TMUX'
)
exe
'silent !echo -ne "\033Ptmux;\033\e]12;"'
. shellescape(color_normal, 1) .
'"\007\033\\"'
let
&t_SI=
"\033Ptmux;\033\e]12;"
. color_insert .
"\007\033\\"
let
&t_EI=
"\033Ptmux;\033\e]12;"
. color_normal .
"\007\033\\"
exe
'autocmd VimLeave * :!echo -ne "\033Ptmux;\033\e]12;"'
. shellescape(color_exit, 1) .
'"\007\033\\"'
else
exe
'silent !echo -ne "\033P\e]12;"'
. shellescape(color_normal, 1) .
'"\007\033\\"'
let
&t_SI=
"\033P\e]12;"
. color_insert .
"\007\033\\"
let
&t_EI=
"\033P\e]12;"
. color_normal .
"\007\033\\"
exe
'autocmd VimLeave * :!echo -ne "\033P\e]12;"'
. shellescape(color_exit, 1) .
'"\007\033\\"'
endif
endif
endif
unlet
color_normal
unlet
color_insert
unlet
color_exit
因为 tmux 的TERM
变量和 screen 的一致,所以得使用TMUX
变量来判断是在 tmux 里还是在 screen 里。
最后,说下指定颜色的方法。可以使用和 HTML 中一样的#rrggbb
甚至简写#rgb
,也可以使用颜色名。这里有个 xterm 的颜色名表。
2011年8月25日更新:
写了个 zsh 函数:
if
[[ $TERM == xterm* ]] || [[ $TERM == *rxvt* ]];
then
# {{{2 设置光标颜色
cursorcolor () {
echo
-
ne
"\e]12;$*\007"
}
elif
[[ $TERM ==
screen
* ]];
then
if
[[ -n
"$TMUX"
]];
then
cursorcolor () {
echo
-
ne
"\ePtmux;\e\e]12;$*\007\e\\"
}
else
cursorcolor () {
echo
-
ne
"\eP\e]12;$*\007\e\\"
}
fi
fi
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK