vimrc - Setting vim options only for files in a certain directory tree?

09
2013-11
  • Andrew Ferrier

    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?

  • Answers
  • Ingo Karkat

    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.

  • suspectus

    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
    else
        if filereadable("../.vimrc_proj")
             so .vimrc_proj
        endif
    endif
    

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

  • ypid

    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


  • Related Question

    vimrc - Why does Vim ignore files in ~/.vim/after/ftplugin?
  • AdSR

    I seem to have a problem with my local Vim configuration but I'm not sure what it is. I need to override some filetype-specific settings. As per multiple tutorials/howtos, I created ~/.vim/after/ftplugin/cpp.vim with appropriate setlocal statements, but it seems not to be read. I checked that ~/.vim/after is in runtimepath.

    I worked around this with autocmd FileType cpp setlocal ... in ~/.vimrc, but that is beyond the point. Any advice on how to diagnose/fix this? Or am I just overlooking something obvious?


  • Related Answers
  • jinfield

    Check the output of:

    :filetype
    

    You may need to add

    filetype plugin on
    

    to your .vimrc (or at least add 'plugin' to your 'filetype' setting).

  • garyjohn

    A good place to start would be to use the :scriptnames command. Open a C++ file however you normally do and execute

    :scriptnames
    

    This will show you the files that Vim has sourced since it was started.