How do I make Vim automatically load a project-specific configuration file from the project directory?
Answer
:set exrc secure
Explanation
Adding set exrc to your vimrc tells Vim to read a .vimrc file from the current working directory when it starts. This lets each project carry its own Vim settings — indentation, makeprg, path options, or any other local customization — without polluting your global config. Always pair it with set secure, which prevents the local config from running shell commands or mappings that could execute arbitrary code.
How it works
exrc— enables reading an extra.vimrc(or.nvimrc/.exrc) from the current directory at startupsecure— when set, the local vimrc can only set options and define mappings; it cannot run shell commands (:!), autocmds, or write commands
For Neovim, project-local Lua configs (.nvim.lua) are supported via the exrc option as well, and Neovim prompts you to trust the file before sourcing it.
Example
In your ~/.vimrc:
set exrc
set secure
In your project's .vimrc (at the project root):
set tabstop=2 shiftwidth=2 expandtab
set makeprg=cargo\ build
set path+=src/**
When you open Vim from that project root, these settings apply automatically for that project only.
Tips
- Add
.vimrcand.nvimrcto your global.gitignoreso project-local configs are not accidentally committed - In Neovim, the
exrcoption with.nvim.luatriggers a trust prompt the first time; use:trustto explicitly allow a file - Use
setlocalinstead ofsetin your project vimrc to scope changes to the current buffer only