How to load different .vimrc file for different working directory?

前端 未结 5 2117
陌清茗
陌清茗 2020-12-14 00:45

I have different option for each working directory. I don\'t want to set these options every time I work. I know I can append vimrc file for the op

5条回答
  •  星月不相逢
    2020-12-14 01:33

    Here's another option, similar to @Ingo Karkat's first suggestion, except that it allows you to have separate vimrc files for each working directory (which is a closer answer to the OP's question). It's a bit hackier than using one of the plugins available, but it does avoid the security problem of the localvimrc plugin, and lets you store all your vimrc variants in your home directory instead of scattered throughout the different working directories (though it would work just as well if you did have them in the root of each of your relevant working directories.

    In .vimrc

    if getcwd() =~ 'One Unique Target Directory'
      source ~/.one_unique_vimrc
    elseif getcwd() =~ 'Another Unique Target Directory'
      source ~/.another_unique_vimrc
    endif
    

    The challenge is that you need to target one vimrc to a unique path - so this wouldn't work so well with the following directory structure - if you've got one vimrc for Foo and one for Baz, there would be some inconsistent behavior ("Why is /Baz/Foo using my /Foo vimrc?!")

    /Foo
    /Foo/Bar
    /Foo/Bar/Baz
    /Baz
    /Baz/Quux
    /Baz/Foo
    

    But it could work well with a directory structure more like this

    /Projects/My Greatest App/
    /Projects/My Greatest App/src
    /Projects/My Greatest App/bin
    /Projects/unique.net/
    /Projects/unique.net/index.html
    /Projects/unique.net/.htaccess
    

    Where you have one vimrc for your 'My Greatest App' and another for your unique website

提交回复
热议问题