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
You can change the working directory with :cd path/to/new/directory . Or you can enter the full path to the location where you want to save the file with the write command, e.g., :w /var/www/filename .
The system vimrc should normally be left unmodified and is located in the $VIM * directory.
Opening vimrc Using file name completion, you could type :e $M then press Tab until you see the desired variable. If you only want to see the path, type :echo $M then press Tab to see the variable, and press Enter. In gvim, the Edit menu includes "Startup Settings" which will use $MYVIMRC to edit your vimrc file.
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:
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:
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.
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
.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With