Moudy
Moudy

Reputation: 888

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

Upvotes: 1

Views: 1330

Answers (2)

eckes
eckes

Reputation: 67047

For project settings, consider using the project plugin: for each project, you could specify an in.vim file whose content gets executed every time you open a file of this project.

Setting a colorscheme there is trivially easy.

Using the project plugin also enables you to set up other project-dependent settings for each individual project:

Other features include:
o Loading/Unloading all the files in a Project (\l, \L, \w, and \W)
o Grepping all the files in a Project (\g and \G)
o Running a user-specified script on a file (can be used to launch an external program on the file) (\1 through \9)
o Running a user-specified script on all the files in a Project (\f1-\f9 and \F1-\F9)
o Also works with the netrw plugin using directory names like ftp://remotehost
(Good for webpage maintenance.)
o Support for custom mappings for version control integration (example of perforce in the documentation).
o I also give an example in the documentation on how to set up a custom launcher based on extension. The example launches *.jpg files in a viewer. I have also set up viewers for PDF (acroread) and HTML files (mozilla) for my own use.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172530

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.

Upvotes: 4

Related Questions