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

前端 未结 5 2118
陌清茗
陌清茗 2020-12-14 00:45

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 op

5条回答
  •  不知归路
    2020-12-14 01:41

    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/.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  
    

    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
    

提交回复
热议问题