0

What I want to do:

Given: I'm using the colorsupport.vim plugin to make my 'gui' colorscheme work in terminal.

And: colorsupport.vim has a command 'ColorSupportSave' that let's me save the 'converted' colorscheme.

Then: On startup, I want to use the 'converted' colorscheme if present, else create it

I would think, that after checking the 'ColorSupport' exists and the converted colorscheme does not, that I could just

      execute 'colorscheme benjamin'
      " then either
      execute 'ColorSchemeSave benjamin-colorsupport'
      " or lower-level
      call s:colorscheme_save("benjamin-colorsupport")

but with the former I get 492: Not an editor command: ColorSchemeSave benjamin-colorsupport and with the latter I get E117: Unknown function: <SNR>19_colorscheme_save

clearly I don't understand how these functions/commands are different from others I'm successfully scripting. (I'm new at this. I've read docs and other questions, but haven't quite figured it out)

Here's how the colorsupport.vim function and commands are defined, in brief

function! s:colorscheme_save(...)
" snip
endfunction
command! -nargs=? ColorSchemeSave :call s:colorscheme_save(<f-args>)

Here are, with more context, the relevant parts of my vimrc

set nocompatible               " be iMproved
filetype off                   " required!

set runtimepath+=~/.vim/bundle/vundle/
call vundle#begin()

Plugin 'gmarik/vundle'
Plugin 'vim-scripts/colorsupport.vim'
call vundle#end()            " required
filetype plugin indent on    " required
if has("gui_running")
  " stuff
else
  if &term != 'cygwin'
    silent !echo "setting 256 color term"
    set t_Co=256
  endif
  let mycolorscheme = 'benjamin'
  " wrap color scheme in gui->term plugins
  if exists('##ColorScheme')
     if filereadable(expand("$HOME/.vim/colors/".mycolorscheme."-colorsupport.vim")) ||
            \    filereadable(expand("$HOME/vimfiles/colors/".mycolorscheme."- colorsupport.vim"))
      silent !echo "using colorsupport.vim with ".mycolorscheme."-colorsupport"
      execute 'colorscheme '.mycolorscheme.'-colorsupport'
    else
      silent !echo "using colorsupport.vim with ".mycolorscheme
      execute 'colorscheme '.mycolorscheme
      " can't figure out how to get this working in the script
      " silent !echo "using colorsupport.vim with ".mycolorscheme."-colorsupport"
      " execute 'ColorSchemeSave '.mycolorscheme.'-colorsupport'
      " or lower-level
      " call s:colorscheme_save("'.mycolorscheme.'-colorsupport")
    endif
   endif
endif

Answers culled from the responses:

refs:

vimrc is loaded before plugins.

If you look at :h initialization you will find that step 3 is load vimrc and step 4 is load plugins.

You can also see that vimrc is loaded before plugins by looking at the output of :scriptnames. scriptnames lists all sourced scripts in the order they were sourced and vimrc is the first thing sourced. (Take a look at :h :scriptnames).

So create the file .vim/after/plugin/colorsupport.vim

it is also possible to achieve the same goal utilizing the autocmd event VimEnter. Because Event VimEnter is performed after all other startup stuff, commands called with this autocmd will take place after the loading of all plugins. The procedure is as following:

First create a function including all the plugin specific scripts in it.

function! g:LoadPluginScript ()
    " ColorSupport {{{
    if exists(":ColorSchemeSave")
      " stuff
    endif
    " }}}
endfunction

augroup plugin_initialize
    autocmd!
    autocmd VimEnter * call LoadPluginScript()
augroup
Community
  • 1
  • 1
BF4
  • 1,076
  • 7
  • 23

1 Answers1

1

Why your attempts don't and can't work

Plugins are sourced after your ~/.vimrc so you can't call functions and commands defined in plugins as they simply don't exist yet. The only exception to this rule is autoload functions but that's irrelevant here.

You can't call :Command or function() if they are not defined directly in your ~/.vimrc or another file explicitly sourced by your ~/.vimrc.

Additionally, the s: prefix in s:colorscheme_save() tells you that the function is scoped to its script and thus can't be called from another one.

What you should do instead

  1. Write your terminal-compatible colorscheme to disk with a clear name, say foobar_term.vim.

  2. Add this little conditional to your ~/.vimrc:

    if has("gui_running")
        colorscheme foobar
    else
        colorscheme foobar_term
    endif
    
romainl
  • 186,200
  • 21
  • 280
  • 313
  • So, what you're saying is that when plugins let you configure them with `let g:somefoo = 1`, the plugin actually depends on the behavior of being loaded after these values are set in the vimrc. But then why does `if exists('##ColorScheme')` return true? – BF4 Nov 04 '14 at 16:21
  • `g:somefoo` is just a completely passive variable left in the global scope for any interested party to use or not *in the future*. `exists('##ColorScheme')` checks if Vim supports the `Colorscheme` event which it does since it is built-in and has nothing to do with any plugin or colorscheme or source order. That check will always return 1 so it is basically useless. – romainl Nov 04 '14 at 16:27
  • What an incredible waste of time. – romainl Nov 04 '14 at 16:31
  • well, if all I cared about what the specific functionality, yes, but generally I'm just trying to get better at vim. – BF4 Nov 04 '14 at 16:37
  • Yeah, getting better at vim is far from a waste of time but getting lost in petty stuff like those colorscheme shenanigans is. – romainl Nov 04 '14 at 16:47