Gcharly
Gcharly

Reputation: 75

How to load a ~/.vimrc then another local .vimrc

I would like to load a ~/.vimrc with general config (color, line number, etc) and then load another .vimrc i have in local file (with specific conf for python file for example).

Upvotes: 3

Views: 4121

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172590

Decluttering / structuring an overly long vimrc

Say, you have a lot of customizations and tweaks, and you'd like a bit more structure. For :set, :map, and :autocmd, you can just put them in a plugin script like ~/.vim/plugin/personal_customization.vim. For plugin customizations (e.g. let g:PluginName_Frobnize = 1), you'd cause order-of-execution issues (unless you choose a plugin directory that comes early in 'runtimepath' or use a special filename (00plugin_customization.vim) that is read first.

filetype-specific settings

Vim has you covered here, but some people are not aware of it; :help usr_43.txt explains filetype-specific settings.

In short, if you only want to enable an option for certain filetypes, use :setlocal option=value, and put the corresponding :setlocal commands into ~/.vim/after/ftplugin/{filetype}.vim, where {filetype} is the actual filetype (e.g. java). (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim.)

Alternatively, you could define an :autocmd FileType {filetype} setlocal option=value directly in your ~/.vimrc, but this tends to become unwieldy once you have many customizations - this may have just brought you here.

file-specific settings

To define special customizations just for a single file, my ModelineCommands plugin can extend modelines beyond the built-in options that can be specified there by default.

For more extensive customizations, it will be better to source an accompanying Vimscript file (e.g. foo.java.vim) that lies next to the file (foo.java):

function! s:AutoSource()
    let l:testedScripts = [expand('<afile>') . '.vim']
    if expand('<afile>:e') !=# 'vim'    " Don't source the edited Vimscript file itself.
        call add(l:testedScripts, expand('<afile>:r') . '.vim')
    endif

    for l:filespec in l:testedScripts
        if filereadable(l:filespec)
            execute 'source' fnameescape(l:filespec)
        endif
    endfor
endfunction
augroup AutoSource
    autocmd! BufNewFile,BufRead * nested call <SID>AutoSource()
augroup END

Alternatively, the following local configuration approaches can also be applied to individual files (by switching on the current filename).

local / project-specific configuration

This often comes up in the form of using different configurations for work vs. private stuff, different projects, etc. There are different approaches:

Local config - central configuration

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

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

It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....

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 (especially with my own enhancements), which even allows local filetype-specific configuration.

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

Upvotes: 5

Amadan
Amadan

Reputation: 198324

A local config would be loaded with a line like this in your .vimrc:

source ~/.vim/local.vim

You could put in that file settings that are particular to your machine, for example.

But settings specific to Python should go into either .vim/syntax/python.vim, or (more likely) .vim/after/syntax/python.vim. You do not need to source them, they will be sourced automatically whenever you open a Python file, as long as you have at least this in your .vimrc:

filetype plugin on

(though I suggest you have filetype plugin indent on instead.)

Upvotes: 6

Related Questions