I'll start by saying that I never wrote any VimScript or done any advanced configuration in my .vimrc. I'm a super noob 😅. Bear with me.

I've been trying to backup my VimWiki on Git. I want to add some redundancy to the Dropbox setup already in place.

I've been following the instructions here:

augroup vimwiki
au! BufWritePost ~/vimwiki/* !git add "%";git commit -m "Auto commit of %:t." "%"
augroup END

For my first test, I saved the wiki index. I got this error:

!git add "/Users/gio/Dropbox/vimwiki/markdown/index.md"
fatal: /Users/gio/Dropbox/vimwiki/markdown/index.md: 
  '/Users/gio/Dropbox/vimwiki/markdown/index.md' is outside repository  at '/Users/gio/dotfiles'

Where /Users/gio/dotfiles is the folder from where I opened Vim.

If I cd to /Users/gio/Dropbox/vimwiki, saving the file results in the commit. But that's annoying, I'd like to be able to open the wiki from anywhere and commit and push the changes on every save.

I tried to add a pushd and popd to the mix, like this:

" Backup on Git
function CommitChangeInWiki()
  !pushd ~/Dropbox/vimwiki
  !git add "%"
  !git commit -m "Auto commit of %:t." "%"

au! BufWritePost ~/Dropbox/vimwiki/**/* call CommitChangeInWiki()

It doesn't work, though. I can see that pushd is called.

I'm clearly missing something here. Thanks for your help.

  • Ben already gave a good answer. Anyway: In Vim you can use :cd <path> and :cd - to jump back.
    – Ralf
    Commented Jun 14, 2020 at 13:46
  • 1
    Welcome, super noob! :D :D
    – B Layer
    Commented Jun 14, 2020 at 21:17
  • Do let the person who answered know if their post helped (by accepting and/or voting) or not (by providing further details). vi.stackexchange.com/help/someone-answers Thanks.
    – B Layer
    Commented Jun 19, 2020 at 17:10

1 Answer 1


The pushd won’t work because it happens in a temporary shell. You could use cd && ..., but the best (I think) is git -C dir command, which makes git run in the provided directory.

  • Thank you @D. Ben Knoble. It took me a while to reply to you because I was experimenting this setup on my spare setup... Anyways, this works thank you :) One thing I'm not sure about is that it worked in the function syntax, but not in one liner one.
    – mokagio
    Commented Jun 28, 2020 at 10:49

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.