0

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." "%"
  !popd
endfunction

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 Jun 14 at 13:46
  • 1
    Welcome, super noob! :D :D – B Layer Jun 14 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 Jun 19 at 17:10
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.

| improve this answer | |
  • 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 Jun 28 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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