0

In my ubuntu gnome-terminal I have vims default syntax highlighting color scheme + some color-tuning made in my gnome-terminal to filter out most colors with green except red, purple and brown. Recently switching to macOS Catalina I found vim has different colors on default syntax highlighting color scheme showing on iTerm2+Zsh.So, I'm looking to have vim tune any(built-in or default) color scheme in .vimrc, so that only these four colors(green+red+brown+purple) are allowed. Can it be done?

My ubuntu syntax highlighting screenshot for reference: enter image description here

My windows(git-bash) syntax highlighting colours (exactly same!): enter image description here

2 Answers 2

1

Your terminal emulator exposes an environment variable, $TERM, that influences what capabilities Vim thinks your terminal has. The relevant capability, here, is how much colors it can display.

The most basic terminal emulators can be expected to support 8 colors (8c), but you can have 16 colors (16c), 256 colors (256c), or even millions of colors (truec) in some recent ones.

In 8c, 16c, and 256c, terminal emulators use an indexed palette for looking up colors. In 8c, the palette has 8 indices, from 0 to 7. In 16c, it has 16 indices, from 0 to 15. Etc.

But…

  • those palettes are not standardized, so there is no guarantee that a given index will look the same everywhere,
  • most terminal emulators allow customizing the lower 16 indices,
  • and there are hacks for customizing the upper 240 ones anyway.

In Vim, the default colorscheme is defined with generic color names, like Brown, which are translated into indices internally. Some of the translations are pretty stable across the spectrum: DarkGreen is always 2 in 8c, 16c, and 256c. But some others vary wildly: Brown is 3 in 8c, 6 in 16c, and 130 in 256c.

And, as we have seen above, those 3, 6, and 130 can be "brown" but they "can" definitely be anything but "brown".

Having a different $TERM in those two terminal emulators is one possible reason for the discrepancies you have to deal with. If one is 16c and the other is 256c, Brown and other colors might look quite different.

You should be able to achieve maximum control over the colors used by Vim for syntax highlighting by:

  • defining the same custom palette across all your terminal emulators,
  • AND making sure $TERM is set to a value that tells Vim to act as if the terminal emulator only supported 16 colors.

With a custom palette, you decide exactly the RGB value of 6, 11, etc.

With a proper $TERM, like the fairly widely supported xterm-16color, you force Vim to always use 6 for Brown and so on.

Note that both things have to be done. If you only define your terminal emulator's palette without forcing Vim to use it, then some or all of your colors will be ignored. If you only make sure that Vim restricts itself to 16 colors without explicitly defining your palette, then the terminal emulator's palette will be used, which can vary from program to program and platform to platform.

Note that I wrote "maximum control", not "total control". There are many other things affecting one's perception of syntax highlighting (font design, font weight, anti-aliasing, colorspace, etc.) that would need to be tackled to achieve "total control".

2
  • Heh. I had no idea, this thing has been so diverse on different platform. But, in my case all I was asking for a few different colors instead of that many. This is so strange, on my ubuntu gnome-terminal: $TERM seems to give xterm-256color, which is exactly same as on my macos-catalina iterm2 output, but the colors are different. But on my git-bash for windows(at office): $TERM gives different output. Only xterm. Yet, vim colors on Git Bash for Windows matches the colors with Ubuntu(gnome-terminal) I'm looking to have. Apr 16 at 17:01
  • You can't expect any of this to look the same everywhere unless you take the matter in your own hands.
    – romainl
    Apr 16 at 17:03
1

I finally managed to set the exact colors:

enter image description here

In iterm2, had to do the following:

  1. set Preferences->Profiles->Terminal->Report Terminal Type to xterm
  2. set Preferences->Profiles->Colors->Minimum Contrast to 0

And then in .vimrc, had to do:

syntax on
"its same colorscheme in all places.
colo delek

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.