简体   繁体   English

Vim:与TMUX结合使用t_Co = 256和term = xterm-256color之间的差异

[英]Vim: Difference between t_Co=256 and term=xterm-256color in conjunction with TMUX

I am testing the various different terminals that I tend to use to SSH into Linux boxes that I have Tmux set up on. 我正在测试各种不同的终端,我倾向于使用SSH连接到我有Tmux设置的Linux机箱。

Basically I noticed this behavior, and I am hoping that somebody could offer an explanation of what's going on. 基本上我注意到了这种行为,我希望有人可以解释发生了什么。 Now it may be the case that this is specific behavior that affects the Prompt app. 现在情况可能是这是影响提示应用程序的特定行为。

I am using Vim within Tmux, and on Panic's Prompt app on my iPhone5 I was having the behavior that 256 colors were not enabling when the .vimrc set the colors using the set t_Co=256 directive. 我在Tmux中使用Vim,在我的iPhone5上使用Panic的Prompt应用程序时,当.vimrc使用set t_Co=256指令设置颜色时,我有256种颜色未启用的行为。 Here, Vim was correctly displaying the colors when it was not run through Tmux. 在这里,Vim在未通过Tmux运行时正确显示颜色。 Also, OS X's Terminal.app correctly rendered the colors (I did not test PuTTY with this on windows unfortunately) with Vim in Tmux. 另外,OS X的Terminal.app正确地呈现了颜色(我没有在Windows上测试PuTTY,不幸的是)在Tmux中使用了Vim。

Then I swapped out set t_Co=256 for set term=xterm-256color and now the colors work when using Vim through Tmux. 然后我换了set t_Co=256set term=xterm-256color ,现在当使用Vim到Tmux时颜色有效。

Note also that I tested both set -g default-terminal "xterm-256color" and set -g default-terminal "screen-256color" settings for Tmux and this change had no effect on behavior. 另请注意,我测试了Tmux的set -g default-terminal "xterm-256color"set -g default-terminal "screen-256color"设置,此更改对行为没有影响。

When you don't use tmux or screen , you only need to configure your terminal emulators to advertise themselves as "capable of displaying 256 colors" by setting their TERM to xterm-256color or any comparable value that works with your terminals and platforms. 当您不使用tmuxscreen ,您只需将终端仿真器配置为通过将其TERM设置为xterm-256color或任何与您的终端和平台兼容的可比值来宣传自己“能够显示256种颜色”。 How you do it will depend on the terminal emulator and is outside of the scope of your question and this answer. 你如何做到这将取决于终端模拟器,超出了你的问题范围和这个答案。

You don't need to do anything in Vim as it's perfectly capable to do the right thing by itself. 你不需要在Vim中做任何事情,因为它完全有能力自己做正确的事情。

When you use tmux or screen , those programs set their own default value for $TERM , usually screen , and Vim does what it has to do with the info it is given. 当你使用tmuxscreen ,这些程序为$TERM设置了它们自己的默认值,通常是screen ,Vim会根据它给出的信息做它所做的事情。

If you want a more uniform (and colorful) behavior, you must configure them to use a "better" value for $TERM : 如果您想要更加统一(和丰富多彩)的行为,您必须将它们配置为使用$TERM的“更好”值:

  • tmux TMUX

    Add this line to ~/.tmux.conf : 将此行添加到~/.tmux.conf

     set -g default-terminal "screen-256color" 
  • screen 屏幕

    Add this line to ~/.screenrc : 将此行添加到~/.screenrc

     term "screen-256color" 

Now, both multiplexers will tell Vim they support 256 colors and Vim will do what you expect it to do. 现在,两个多路复用器都会告诉Vim它们支持256种颜色,而Vim会按照您的预期进行操作。

edit 编辑

My answer assumes that you are able to edit those configuration files but, since you are able to edit your ~/.vimrc , I don't think that I'm that far off the mark. 我的回答是假设您能够编辑这些配置文件,但是,由于您可以编辑~/.vimrc ,我认为我不是那么遥远。

edit 2 编辑2

The value of the term option (retrieved with &term ) is the name of the terminal as picked up by Vim upon startup. term选项的值(使用&term检索)是Vim在启动时获取的终端的名称。 That name is what you are supposed to setup in your terminal emulator itself. 该名称是您应该在终端模拟器中设置的名称。

The value of the t_Co option ( &t_Co ) is what Vim considers to be the maximum number of colors that can be displayed by the host terminal. t_Co选项( &t_Co )的值是Vim认为是主机终端可以显示的最大颜色数。 It is defined according to the entry corresponding to $TERM in terminfo : 它是根据terminfo $TERM对应的条目定义的:

 term            | t_Co
-----------------+------ 
 xterm           | 8
 xterm-256color  | 256
 screen          | 8
 screen-256color | 256

When Vim starts up, it gets the value of the TERM environment variable, queries the terminfo database with that value and stores a number of informations on its environment in several t_… variables among which… the number of colors available in t_Co . 当Vim启动时,它获取TERM环境变量的值,使用该值查询terminfo数据库,并在其环境中以多个t_…变量存储多个信息,其中... t_Co可用的颜色数。 Given a "legal" terminal type (one that Vim can lookup), Vim always assumes the correct number of colors. 给定“合法”终端类型(Vim可以查找的终端类型),Vim 总是假设颜色正确。

Setting t_Co to 256 while leaving term to its Vim-defined value — or, more generally, setting t_Co and/or term to values that don't match with the host terminal — makes no sense and will likely create troubles when Vim sends a signal that is not understood by the terminal or vice-versa. t_Co设置为256同时将term设置为其Vim定义的值 - 或者更一般地,将t_Co和/或term设置为与主机终端不匹配的值 - 没有任何意义,并且当Vim发送信号时可能会产生麻烦终端不理解,反之亦然。

While it is entirely possible to do so, messing with t_Co and term in Vim is both totally useless and possibly harmful. 虽然完全有可能这样做,但在t_Co弄乱t_Coterm既完全没用,也可能有害。

Again, just setup your terminal emulators and terminal multiplexers correctly. 再次,只需正确设置终端仿真器和终端多路复用器。 That's really all you need. 这就是你真正需要的一切。

If you end up in a terminal multiplexer or a terminal emulator where you can't define a correct TERM , then and only then you can force Vim to assume 256 colors. 如果您最终在终端多路复用器或终端仿真器中无法定义正确的TERM ,那么只有这样您才能强制Vim采用256色。 To that end, changing the value of t_Co is the only thing that makes sense: 为此,改变t_Co的值是唯一有意义的事情:

if &term == "screen"
  set t_Co=256
endif

So… if you can configure each individual part: 所以...如果你可以配置每个单独的部分:

  • terminal emulator: xterm-256color 终端模拟器: xterm-256color
  • tmux/screen: screen-256color tmux / screen: screen-256color
  • vim: nothing vim:什么都没有

and you are done. 你完成了

If you can't control every part, use a simple conditional in your ~/.vimrc to set t_Co according to &term but don't change the value of term . 如果你无法控制每个部分,可以在~/.vimrc使用一个简单的条件来根据&term设置t_Co但不要更改term的值。

But if you can edit a ~/.vimrc there's no reason you can't edit a ~/.screenrc or ~/.tmux.conf or ~/.bashrc or whatever. 但是如果你可以编辑一个~/.vimrc就没有理由不能编辑~/.screenrc~/.tmux.conf~/.bashrc等等。

You can use both set t_Co=256 and set term=xterm-256color together. 您可以同时使用set t_Co=256set term=xterm-256color

term tells Vim what terminal type to use, which controls the display/rendering of all aspects of Vim, including how to map key input, redraw the screen, move the cursor, display colors, etc. Normally, Vim can figure this out on its own via the TERM environment variable provided by your OS. term告诉Vim使用什么终端类型,它控制Vim各个方面的显示/渲染,包括如何映射键输入,重绘屏幕,移动光标,显示颜色等。通常,Vim可以在其上找到它通过您的操作系统提供的TERM环境变量拥有。

It's often helpful to set it explicitly in case the OS value is incorrect. 在操作系统值不正确的情况下,明确设置它通常很有帮助。 This is especially true if you're connecting over a network from a terminal emulator that doesn't provide the correct value. 如果您从未提供正确值的终端仿真器通过网络连接,则尤其如此。

t_Co is one of many terminal options (used by the termcap system that Vim uses for terminal control). t_Co是许多终端选项之一(由Vim用于终端控制的termcap系统使用)。 It specifically refers to the number of colors the terminal supports. 它具体指的是终端支持的颜色数量。 Sometimes you need to override this if the terminal emulation is mostly correct, but Vim isn't correctly identifying the number of colors supported. 如果终端仿真大部分正确,有时你需要覆盖它,但是Vim没有正确识别支持的颜色数。

I use both of these options in my .vimrc to ensure that Vim uses 256 colors in tmux using all the terminals I like (Ubuntu's gnome-terminal, OSX's iTerm2, and Windows's KiTTY). 我在.vimrc使用这两个选项,以确保Vim使用我喜欢的所有终端(Ubuntu的gnome-terminal,OSX的iTerm2和Windows的KiTTY)在tmux中使用256种颜色。 I also have most of those terminals explicitly configured to send xterm-256color as their terminal type. 我也有大多数明确配置的终端发送xterm-256color作为其终端类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM