12

Original post

I've got a colorscheme configured in my .vimrc but sometimes I'd like to turn it off completely which means that I would like Vim to use the default background and foreground colors of my terminal.

My problem is that I don't know how to easily do it. When I use :syntax off Vim turns off most of its coloring but it doesn't remove the cursorline color. It doesn't change the background color to the terminal's default one.

Is there a command like :nocolorscheme or vim -u NONE but just for the colors?

I am aware of the fact that I can create a custom .vimrc file and load it with the -u option. I am looking for an easier solution though.

Major update

Because you've suggested me a number of different commands and approaches I decided to show you some photos of the screen after calling those commands.

Terminal envirnoment

I use st(1) terminal from suckless.org with a custom config.h so that I've got defaultfg = 10 (bright green) and defaultbg = 0 (black). I'm running the GNU/Linux version that PocketC.H.I.P. ships with (uname -a results in Linux chip 4.3.0-ntc #1 SMP Wed May 11 21:57:30 UTC 2016 arm7l GNU/Linux).

Screenshots

  1. No colorscheme (this is what I want)

    enter image description here

    This is what I want to achieve without having to start Vim with vim -u NONE.

  2. Normal colors

    enter image description here

    I get these colors when:

    • I normally start my Vim (this is my standard colorscheme).
    • I call :set t_Co=0.
      It is worth noting that it doesn't change anything when I use my custom monokai colorscheme. It does change the appearance in the default colorscheme as you can see in 5.

      The funny thing is that the output of :verbose set t_Co? called subsequently is t_Co=256 which indicates that literaly nothing has changed.

    • I start Vim with TERM=xterm vim.

  3. Default colors

    enter image description here

    I get these colors when:

    • I call :colorscheme default.
  4. Vim removed most of colors but doesn't fall back into terminal's defaults

    enter image description here

    I get it after:

    • Calling :syntax off.
  5. Almost no colorscheme

    enter image description here

    I get this after running these 4 commands one after another:

    " The effect is more visible on the default colorscheme 
    " than on my molokai colorscheme.
    :set t_Co=0
    :colorscheme default
    :hi clear
    

    When I run :hi LineNr afterwards I get:

     LineNr          XXX term=underline ctermfg=6 guifg=Brown
    
  6. Strings and types underlined in a C source file

    enter image description here

    After applying :set term=vt100.

  7. Broken redrawing in Vim

    Video thumbnail
    Vim broke after calling :set term=builtin_dumb (video)

    After applying :set term=builtin_dumb my Vim forgets how to redraw lines as I move around using j and k.

  8. Examine the defaults of my temrminal

    enter image description here

    This is what I get after running echo -e " \033[0;4mfoo\033[0;0mbar" in my terminal.

14
  • 3
    I think you describe two different things: If you want to disable the colorscheme you will not have your terminals colors but the default colors of vim. If you want to have your terminal colors in vim you'll have to find (or create) a colorscheme matching your terminal's one.
    – statox
    Commented Jul 13, 2016 at 7:37
  • @statox OK, I just thought that when I run vim -u NONE it inherits the terminal's defaults. Thanks for pointing that out. Commented Jul 13, 2016 at 7:48
  • If only it was that easy I think a lot of people would be pretty happy ;-) But I think it is not possible for at least to reasons: 1) not all terminal define their colors the same way 2) Vim has its own color mechanism which is hardly compatible with the existing mechanisms for terminals. Now I'm really not an expert in color customization so maybe a colorscheme guru will be able to be more specific or will know a way :-)
    – statox
    Commented Jul 13, 2016 at 7:51
  • 1
    I've updated my answer to account for underlining. I should have been a bit more clear in my previous comment: I understand that the t_Co setting appears to have different effects depending on your colorscheme, although I have no idea by what mechanism this might occur. One more thing to try: When using monokai, run :set t_Co=0, immediately followed by :verbose set t_Co? What is t_Co set to? (I'm wondering if somehow it's not getting set correctly: my only guesses are that possibly you've got an autocommand that resets it, or there is a bug that is causing it not to be set.)
    – Rich
    Commented Jul 20, 2016 at 8:36
  • 1
    Okay, mystery solved: see my update as to why :set t_Co=0 doesn't work when the colorscheme is set to monokai.
    – Rich
    Commented Jul 22, 2016 at 8:31

4 Answers 4

9

Disabling Colours Entirely

If you want Vim not to use colours at all, you just need to run the command:

:set t_Co=0

This tells Vim that it is running in a terminal that does not support colours, so Vim will only output black and white text (which will then be displayed by your terminal in its default colours).

However, note that after changing the t_Co setting the current colorscheme is reloaded (so that it can make appropriate changes to the colours it defines). The "monokai" colorscheme you are using starts with the line :set t_Co=256, immediately resetting the option.

I'd argue that this is incorrect behaviour, and should be filed as a bug with the maintainer of that colorscheme. Of course, they might well disagree and argue that as their colorscheme requires 256 colours, it doesn't make sense to try and use it in a terminal which does not support this (and that this line helps monokai work correctly when Vim is running in an incorrectly configured terminal).

Either way, you will need to change colorscheme away from monokai first before setting the t_Co option, as you have discovered.

Restricting Colours to Those Used Commonly in the Shell

The colours that Vim uses for its output can be defined and viewed with the :highlight command. Running :highlight clear will reset these to Vim's defaults (effectively the same as running :colorscheme defaults)

Excerpt from :help :highlight:

:hi[ghlight] clear        Reset all highlighting to the defaults.  Removes all  
                          highlighting for groups added by the user!
                          Uses the current value of 'background' to decide which
                          default colors to use.

(N.B. Note the comment about the 'background' option.)

If this doesn't produce the output you desire, you have a couple of options.

  1. If you're currently using Vim in 256-colour mode, you could try using 16-colour mode instead, as this would result in Vim selecting from the palette of colours that you are more likely to see when in the shell.

    The clean way to do this is by running Vim with a different $TERM variable set. The value you need to use depends on your terminal, but, for example, if your $TERM is usually xterm-256color, then running vim with the following command will do the trick:

     TERM=xterm vim
    

    (You can also achieve a similar effect in a more hacky fashion by setting the terminal option directly inside vim: :set t_Co=16.)

  2. Alternatively if it's only a handful of particular items that are coloured incorrectly, you could just change the colours of those items with the :highlight command. e.g. to remove the background colour from the cursor line:

     :highlight CursorLine ctermbg=NONE
    

    If you're not sure which group you need to change, you can either just run :highlight with no arguments or you can run: :so $VIMRUNTIME/syntax/hitest.vim to open a new window displaying all current highlighting.

    See the sections at the bottom of this answer for tips on how to make doing this quick with a custom command or by setting up a new colorscheme.

Background Colour Erase (BCE)

If you have a problem with the background looking different when it contains text, you might be encountering BCE (Background Color Erase). You can disable this with the command:

:set t_ut=

There is a specific question about this here, and the problem is covered in more detail in this blog post.

Update (Overriding Underlines):

From our discussion in the comments, it's become clear that one of the problems you have is that your terminal displays underlined text in a different colour. You have two options to workaround this:

  1. Fix your terminal configuration so it renders underlined text in the same colour! I can't help you with this.

  2. Turn off the underlining in Vim. You can do this with the :highlight command: e.g. the following will remove all colour/formatting from the line numbers:

     :highlight LineNr NONE
    

Switching the Colours Off with a Command

In order to make this quick to do, you could set up a command to do all the required steps in one go:

 function! TurnOffColors()
   :set t_Co=0
   :highlight LineNr NONE
   :highlight CursorLine NONE
   " Add any other necessary highlight lines here
 endfunction
 
 command! TurnOffColors call TurnOffColors()

Creating a New colorscheme

Alternatively, you could create a new "NoColorsNoUnderlines" colorscheme where you turn off underlines as appropriate. For instructions for how to do this, run the command:

:edit $VIMRUNTIME/colors/README.txt

You could use this black and white colorscheme as a starting point.

7
  • It looks like it just sets colorscheme default. Together with syntax off it is almost what I desire. Except that (1) the cursorline is still underlining the current line and colors it white; (2) the line numbers are still colored. Commented Jul 13, 2016 at 10:31
  • 1
    @MateuszPiotrowski I've expanded my answer somewhat, which hopefully will help. Note that unless you're turning off colours entirely, it doesn't really make sense to say that you don't want Vim to use a colorscheme. Vim displays stuff on screen, and it has to use some colours. The colours are defined by the colorscheme.
    – Rich
    Commented Jul 13, 2016 at 10:59
  • @MateuszPiotrowski "LineNr" is the highlight setting that affect the colours of the line numbers.
    – Rich
    Commented Jul 13, 2016 at 11:00
  • 3
    Actually, re-reading your question, it looks like maybe you do just want to turn off the colours entirely. :set t_Co=0 will do that for you.
    – Rich
    Commented Jul 13, 2016 at 11:35
  • None of these options work as I expect. vim -u NONE starts with terminal's background and foreground but it doesn't load any vimrc. I wonder what should I disable to get this effect. Commented Jul 15, 2016 at 10:12
4

set t_Co=0 breaks certain Vim features, like ^X^O and still uses basic term highlighting (bold, underlined).

You could use something like nofrils and adjust the hi Normal cterm=none ctermfg=none ctermbg=none. Other than that I don't see any other way, without messing up your Vim experience.

2

I'm suspecting the clean solution could be to create a fully monochrome colorscheme and go with that. However even with one, there is always the risk of falling back to defaults if missing categories, or if new categories are added in newer vim versions.

This is function is admittedly a bit hackish, but it seems to work to make my screen readable in sunlight:

function! Utomhus()
    set guifont=Iosevka\ Term\ Bold\ 20
    colorscheme default
    set t_Co=0 " Goes to greyscale in terminals, does nothing in gui.
    " syntax off     " Using `syntax off` breaks folding. :(
    redir =>output
    silent highlight
    redir END
    for line in split(output, "\n")
        let category=split(line)[0]
        if match(category, '=') > 0
            continue
        endif
        exec 'highlight ' . category . ' NONE'
    endfor
    exec 'highlight Cursor guibg=Black guifg=White'
    exec 'highlight NonText guifg=Grey'
endfunction

That function starts with (unrelatedly) setting a huge font, and doing a few things explained well in other answers. Note how it avoids to turn off syntax, since that has the horrible side-effect of breaking folding. Then the output of :highlight is captured, and each and every category is configured to not use colors. To handle the fact that some lines might get wrapped, all words containing an equal sign are assumed to be an argument rather than a category and gets skipped. Finally, the cursor needs to be in reverse video to be visible, and NonText makes sense to make half-bright.

1
  • Execute in the last few commands is unnecessary.
    – D. Ben Knoble
    Commented Jan 2 at 16:48
-1

syntax off in .vimrc
:syntax off in a session
This works in Vi or Vim for me.

1
  • The Question mentions this is not a plausible solution.
    – D. Ben Knoble
    Commented Jan 4 at 14:30

Your Answer

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

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