Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a few projects that need different commands for running, building and testing, so I've put a .vimrc file in each of the project directories that define mappings to some F buttons. In the vim help files it says:

'exrc' 'ex'             boolean (default off)
                        {not in Vi}
        Enables the reading of .vimrc, .exrc and .gvimrc in the current

So I have that option enabled, but when I :cd into a directory, the .vimrc file is not loaded. If I write :so .\.vimrc then it loads fine. Why isn't it being loaded automatically?

I'm using gVim 7.3 x64 for Windows.

share|improve this question
Automatically using a .vimrc file from the current directory is a really bad idea. That file could easily be from an archive that you had downloaded, and automatically sourcing it would allow whoever created that archive to run arbitrary code on your computer. – qqx Jan 11 '13 at 15:07
@qqx: I think I can keep that in mind, the same way I keep in mind not to execute binaries I don't trust :P – Hubro Jan 11 '13 at 15:23
set exrc never worked for me. You could try this plugin. There are probably others on – romainl Jan 11 '13 at 16:09
up vote 3 down vote accepted

If 'exrc' is set, the current directory is searched for a file named _vimrc, .vimrc or _exrc at startup. This is not done every time you :cd to a new directory. See

:help .vimrc
share|improve this answer
Doesn't that make it sort of useless? :S At least for gVim. Is there a gVim.exe startup parameter I can use to choose startup directory? That way I could create one shortcut for each project. Or even better, maybe I could specify a vim config file in the application shortcut so I can avoid the exrc thing altogether. Google here I come – Hubro Jan 11 '13 at 17:11
It doesn't help with gvim launched from your desktop or file manager, but it wasn't intended to help with that. What you need is something that loads a vimrc from the parent directory of the first file you open when starting gvim. In that case, you know that the file name will be passed as an argument, so you could test for that and source %:h/.vimrc. See the :help entries for argv() and fnamemodify(). I wrote a plugin for this that looks at the path of every file when it's opened and sources a project-dependent file of buffer-local settings. – garyjohn Jan 11 '13 at 18:17

Here's something you could try:

autocmd BufEnter{,other event} {full path to file} {do blah}

And you would put that into an auto command group:

augroup {name you want here}
    autocmd BufEnter{,other event} {full path to file} source {full path to vimrc file}
    autocmd BufEnter{,other event} {full path to file} {define mapping here}
    autocmd BufEnter{,other event} {full path to file} {do blah}
    autocmd BufEnter{,other event} {full path to file} {do blah}
augroup END

So you would put your settings here, this way if you're in a specific buffer those mappings will load.

I also recommend doing some BufLeave events in order to unset those mappings when you're not in those buffers, if you want.

would that work for your needs? I'm not sure exactly of what you need so I simply made a template for you. :)

As I showed, you can do it in various ways, you decide which is better for your needs. Sorry I'm not actually solving the underlying problem of that setting not working though. :(

share|improve this answer
I'm not sure if you're joking... I say in my question that that's exactly what I'm doing now. – Hubro Jan 11 '13 at 14:27
@Codemonkey /facedesk 5 times. I missed that last part. Wait just a sec, I'm gonna edit my question to use autocommands (if I find an event to do that). :) – greduan Jan 11 '13 at 14:30
@Codemonkey OK I changed my answer. Would that work better for you? – greduan Jan 11 '13 at 14:39
I'd really like to know why the exrc isn't working like advertised though. Have I misunderstood what the option means? – Hubro Jan 11 '13 at 14:55
OK, some things to check. Do you have set exrc in your actual .vimrc file? Also, instead of a .vimrc file, did you know you can also use a .exrc file for these customizations? – greduan Jan 11 '13 at 15:25

Just for the record, what I ended up doing was creating a short cut for each of my projects and loading a project specific config file as a session file using -S session.vim.

share|improve this answer

In my opinion this is best variant :

if getcwd() =~ '^\(/my/safe/dir1/\|/my/safe/dir2/\)'
  set secure exrc

Source from: but fixed syntax

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.