1

I have my .vimrc with some global/local configurations, but when I am working in a project, I know that I often create configs (settings, mapping, command, abbreviations, etc.) specifically for this project.

I usually create a session to make them accessible in the directory where I use them with:

:mks!

That create a Session.vim file where my specific configurations are (see :help mksession).

I am asking myself if there is a better/simpler way to do so for my use case (if it's possible, a way to load directly my saved settings when I open a file with vim in the same directory).

2

1 Answer 1

2

Off the top of my head perhaps something like this in your vimrc...

let g:localconffile = '.myvimconf'

func! LoadLocalConfig() abort
    let l:fname =  expand('%:p:h') . '/' . g:localconffile
    if filereadable(l:fname)
        exe "source " . l:fname
    endif
endfunc

augroup localconf
  autocmd!
  au VimEnter * call LoadLocalConfig()
augroup END

This will look for a file named .myvimconf in the directory of the file being opened and if it's found it will load it (like an additional vimrc file).

VimEnter is going to trigger this only when you first open Vim. If you want this to happen for each file opened we'd use something like BufReadPost.

This is just the basic idea. You may want to refine and tighten it up. First let's see if it's even the kind of thing you're looking for.

3
  • 1
    Nice. It is secure and adjustable. Commented Jan 10, 2021 at 20:45
  • B Layer, can I get your input on the possible dupe I linked above?
    – D. Ben Knoble
    Commented Jan 10, 2021 at 23:17
  • @D.BenKnoble Yeah, that certainly covers the same ground. I have no problem with this being marked dupe.
    – B Layer
    Commented Jan 11, 2021 at 1:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.