How to load different .vimrc file for different working directory?

vim
27,362

Solution 1

If each project uses a distinct language, the built-in filetype plugin (ftplugin) mechanism, as described by Jamie Schembri, will work just fine. If you really need different settings for the same type of files, read on:

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.

Solution 2

Filetype Plugin

This is what you're probably looking for, and is a very neat approach. You'll need to set filetype plugin on in your vimrc to get this to work. A file must then be created at ~/.vim/ftplugin/<language>.vim which will be loaded automatically for any buffers using that language.

For example, instead of writing your JavaScript settings to ~/.vimrc_js, write them to ~/.vim/ftplugin/javascript.vim.

Autocmd

autocmd is the simplest way to set something on a language-specific basis:

autocmd Filetype <language> <setting>

This goes directly in your vimrc and will load settings for a specified filetype only.

To enable spellcheck across various text files, for example, one could use:

autocmd FileType markdown,tex,textile setlocal spell

You can set multiple settings at once by separating them with a pipe, those this quickly becomes unwieldy:

autocmd FileType php setlocal shiftwidth=4 tabstop=4|let php_sql_query=1

AutoCMD + Source

On rare occasions you might have enough settings to warrant a separate file, but would like to load them for multiple languages. Using filetype plugins, you'll end up with duplicate files or symlinks.

A simple alternative is to fall back to autocmd, but instead of writing the settings in one big line, you can instead source a file. For example:

autocmd FileType markdown,tex,textile source ~/.vim/lang_settings/text.vim

Solution 3

You can try the localvimrc plugin. It tries to load .lvimrc in your project directory or the directories above. You can put your settings there for each project.

Solution 4

Another solution is EditorConfig. It sets coding styles for different projects or filetypes. It works with Vim, but it is editor agnostic, so you can use the .editorconfig file with other text editors (SublimeText2, Notepad++, Vim, Emacs, Textmate, Gedit) or IDE (idea-based IDE's, RubyMine, PHPStorm, Visual Studio).

Share:
27,362
Amp Tanawat
Author by

Amp Tanawat

A coding lover, team-working addict, hyper productivity seeker. Want to deliver a great product(s) with great teammates to customers. Use Java as primary programming language, fun with playing on JavaScript, enjoy to jump into others programming platforms.

Updated on June 05, 2020

Comments

  • Amp Tanawat
    Amp Tanawat almost 4 years

    I have different option for each working directory. I don't want to set these options every time I work. I know I can append vimrc file for the options but I don't want to use the same configuration in every directory. How can I do with this situation?

    Example:

    For javascript project, I want to load settings from ~/.vimrc_js

    For Python project, I want to load settings from ~/.vimrc_py