Search code examples
vimterminalgnu-screenxtermgnome-terminal

Why is the default vim's background option is differrent when run in gnome-terminal and gnu-screen?


I'm using Ubuntu 16.04 LTS with vim 7.4 and screen installed. My question is why when I open vim in the default ubuntu terminal the background option is dark while open it in gnu-screen it is light. You can see subtle different in syntax highlighting when open in those terminals.

Where did it get that values?

Let vim guess for the background values both gives light. (i.e :set bg& bg? )

I've been dive through the vimfiles but found no suspect.

Maybe it lies somewhere in vim's config files but I've not found out yet or is there another mechanism of setting the default background option in vim?


Summary

$ vim
:set bg?
  background=dark
:set bg&
:set bg?
  background=light
:q
$ screen
$ vim
:set bg?
  background=light   ???
:set bg&
:set bg?
  background=light

Solution

  • vim's using an escape sequence from xterm's repertoire to ask what the background color is, which you can see in the block in term.c beginning with the comment

        /* Check for background color response from the terminal:
         *
         *       {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
         *
         * {lead} can be <Esc>] or OSC
         * {tail} can be '\007', <Esc>\ or STERM.
    

    culminating in some code (which I'd recommend rewriting...):

                {/* TODO: don't set option when already the right value */
                    LOG_TR("Received RBG");
                    rbg_status = RBG_GOT;
                    set_option_value((char_u *)"bg", 0L, (char_u *)(
                        (3 * '6' < tp[j+7] + tp[j+12] + tp[j+17])
                         ? "light" : "dark"), 0);
    

    which was added in mid-2015:

    commit b5c3265521749fda81ae4b052de35a0d1209cf50                                 
    Author: Bram Moolenaar <Bram@vim.org>                                           
    Date:   Thu Jun 25 17:03:36 2015 +0200                                          
    
        patch 7.4.757                                                               
        Problem:    Cannot detect the background color of a terminal.               
        Solution:   Add T_RBG to request the background color if possible. (Lubomir 
                    Rintel)
    

    xterm patch #94 (March 1999) added the control sequence for querying colors:

    extend dynamic colors control sequences to allow users to determine the colors and font which are currently active.

    VTE's developers copied the feature early in 2014 (see #567444).

    But GNU screen doesn't recognize the sequence (or its response), so it doesn't allow it to pass through.

    By the way, there's more than one way that vim could have done this. I checked to see which method it used with strace.