Vim Directory Environment Configuration

From Schmid.wiki
Jump to: navigation, search

Idea

The idea is really simple. You create a .vimrc for a project directory and vim parses it whenever a file from that directory is opened. It enables some very nice features:

  • Automatic setting of makeprg for certain filetypes for this directory.
  • Per-file configuration of other Vim options inaccessible by modelines.
  • Automatic loading of a default file (e.g. README) if no file is specified on the Vim command-line.

The possibilities are virtually endless.

Configuration

Really simple, just add the following to your ~/.vimrc:

" Vim Directory Environment Configuration
set exrc

Now, whenever you create a .vimrc file in a directory, it will be sourced by Vim after loading the global vim config but before loading any plugins. (see :h startup for more information).

To avoid your ftplugin settings ruining your .vimrc settings, you could check if they are already defined:

if !exists("&makeprg")
    set makeprg=rst2html.py\ %\ %.html
endif

Example .vimrc

" check current filename: if no file was opened, open README
if @% == ""
    silent open README
endif

" check current filename: if README, we must have blue colorscheme
if @% == "README"
    colorscheme blue

" check filetype: if C++, set makeprg
elseif &filetype == "cpp"
    set makeprg=g++\ -c\ %

" check file extension (file ends with .rst):
elseif match(@%, "\.rst$") != -1
    set makeprg=rst2html.py\ %\ %.html
endif
Personal tools