Search code examples
vimcolor-scheme

How to set VIM colorscheme based on directory?


I often switch between different apps would like each project to have it's own colorscheme so its easier to tell them apart.

I want to put something like the following in my .vimrc but I'm having trouble with the VIM scripting syntax.

# if I were to write it in Ruby
case current_path
when '/path/to/project'       then color textmate
when '/path/to/other_project' then color ir_black
end

Solution

  • As long as you don't want to mix different projects in a single instance of Vim (colorschemes can only be set globally, not for individual windows!), this is trivial to translate to Vimscript:

    if getcwd() ==# '/path/to/project'
        colorscheme textmate
    elseif getcwd() ==# '/path/to/other_project'
        colorscheme ir_black
    endif
    

    For an extensive configuration, I would probably use a Dictionary lookup in a loop instead, but I've kept this intentionally simple.

    To handle subdirectories, use this for the comparison:

    if stridx(getcwd(), '/path/to/project') == 0
    

    You can also do regexp matching via =~# instead of ==#.


    Alternatively, there are a couple of local vimrc plugins, which allow you to apply project-specific settings to certain subdirectories. I use localrc for this.