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.

  • 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 vim.org. – romainl Jan 11 '13 at 16:09

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
| 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. :(

| 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

In vim version 8.1^ there is a autocmd-event named DirChanged that you can use to eval your project specific .vimrc. Here is more detail about when this event is triggered:

DirChanged = Working directory has changed in response to the |:cd| or |:lcd| commands.

Working Example

I'm not an expert in vim-script but I created the following function that loads my project specific .vimrc file, once I cd into the project folder. Past this function into your main ~/.vimrc file.

if !exists("*LoadProjectVimrc")
  function! LoadProjectVimrc()
    let vimrcFile = findfile(".vimrc", ".;")

    if !empty(vimrcFile)
      execute ":so" l:vimrcFile
      echom l:vimrcFile

autocmd DirChanged * :call LoadProjectVimrc()
| improve this answer | |

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

| improve this answer | |

In my opinion this is best variant :

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

Source from: http://vim.wikia.com/wiki/Open_vimrc_file but fixed syntax

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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