0

I am trying to execute some custom settings based on the directory on startup. For this I am trying:

autocmd VimEnter /path/to/dir/* :source /custom.vim

When I now execute:

cd /path/to/dir
vim

I would expect the config to have been loaded. This does not seem to work. However, this works:

autocmd VimEnter * :source /custom.vim

Is there a way to load a config once on startup based on the directory that vim was started in?

Note: I am aware of Applying settings to a directory tree only but would prefer to make it work this way.

1 Answer 1

2

Try this one:

augroup DIR_CONFIG | au!
    au VimEnter * if getcwd() == expand('~/temp') | source my.vim | endif
augroup END

PS, as @eclipse has mentioned in the comments one could just add to vimrc:

if getcwd() == expand('~/temp') | source my.vim | endif
6
  • Ah, crap...this time you beat me by a lot more than a few seconds. It didn't show up until just now, though. :( Will delete.
    – B Layer
    Commented Sep 29, 2021 at 13:28
  • 1
    np, we came up to essentially the same answer, indeed.
    – Maxim Kim
    Commented Sep 29, 2021 at 13:29
  • 1
    Well, great minds think alike, I always say. +1 :)
    – B Layer
    Commented Sep 29, 2021 at 13:37
  • 2
    Or just if getcwd() =~# '/some/.*/path' | ...
    – B Layer
    Commented Sep 29, 2021 at 13:44
  • 1
    Btw: could I skip the autocmd all together and just used the if like so: if getcwd() == expand('~/temp') | source my.vim | endif
    – eclipse
    Commented Sep 29, 2021 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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