Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

There is a programming project I work on where everyone else uses a tabsize of 4, so I find it most straightforward to set tabstop=4 in my ~/.vimrc. However, I'd rather not have this affect every file I edit - just those for this project - let's say every file in a certain directory (and its subdirectories).

Is there a way I can easily conditionally set variables based on the directory prefix of the file?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

Central configuration

If it's okay to configure the local exceptions centrally, you can put such autocmds into your ~/.vimrc:

:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4

On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:

Local config with built-in functionality

If you always start Vim from the project root directory, the built-in

:set exrc

enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.

Local config through plugin

Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin, which even allows local filetype-specific configuration.

Note that reading configuration from the file system has security implications; you may want to :set secure.

share|improve this answer
The third solution (the plugin) is excellent, and exactly the kind of thing I was looking for. I'm trying to avoid putting the config centrally, so you're right that the first is not ideal. Thanks for a comprehensive answer. –  Andrew Ferrier May 23 '13 at 11:05
Great! I also use the localrc plugin; it's truly useful. –  Ingo Karkat May 23 '13 at 11:08
add comment

You can configure vim to read further commands using the source (so) command. Add this to your ~/.vimrc - it searches the current directory and if .vimrc_proj file not found there searches for .vimrc_proj in the parent directory.

if filereadable(".vimrc_proj")
    so .vimrc_proj
    if filereadable("../.vimrc_proj")
         so .vimrc_proj

Then add any custom commands in .vimrc_proj config files to suit your projects.

share|improve this answer
I think that only works if I'm in the project's directory when I open the file, right? This doesn't handle where the current directory is something completely different. –  Andrew Ferrier May 23 '13 at 11:00
Correct, that is a limitation –  suspectus May 23 '13 at 11:01
add comment

You can use a plugin for Vim to solve the problem in a more general way by trying to detect the indention.

The plugin of choice for me is DetectIndent. It took some time for me to test all the forks of the plugin to find one that suits my needs. The original one was really close but not quite so I made my own fork.

For debugging it is very helpful to :set verbose=1 and run the plugin again with :DetectIndent

share|improve this answer
I think you may have put this answer on the wrong question? It doesn't seem relevant. –  Andrew Ferrier May 25 '13 at 20:49
@AndrewFerrier This answer is perfectly in place, it just presents different attitude to the problem. Instead of setting tab size for specific files you can use a plugin to detect correct tab size for each file. –  gronostaj May 25 '13 at 21:01
@gronostaj my question wasn't really about setting tab sizes, it was about per-directory-tree configuration - the tab size was just an example. But I see what you were getting at now, and it is a useful tip. Thanks. –  Andrew Ferrier May 25 '13 at 21:03
add comment

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.