I am new to the world of Vim, and I want to make it so that every time I save a file it is commited to version control. Is this possible?.
I use a vim autocommand which I place in my .vimrc. The command first does a crude check that we're working in a git directory (by checking existence of a .git directory, either in the current directory or using
git rev-parse), then uses
git -a -m % so that only files that have previously been added are staged and committed.
autocmd BufWritePost * execute '! if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m %; fi'
- 1There is missing single quote at the end of the cmd, right after fi. – Andrew-Dufresne Sep 25 '12 at 15:37
- 2This version works only if you are in the root directory of your repo. A better solution is
autocmd BufWritePost * execute '! if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m %; fi', from a SO answer – Andrew-Dufresne Sep 25 '12 at 15:39
- This works for me on linux. Any idea how to make it work on windows? – user334287 Feb 9 '18 at 7:09.
- That's true, but I also want to have an overnight script that moves files that successfully compile from the "save-on-commit" repo to a "meaningful change" repo. – gonzo.floyd May 20 '11 at 15:19
- Well, it's not entirely a bad idea with Git. "Commit frequently" is kind of a motto. It works better this way, too, because you can work concurrently on the same file with multiple developers and minimal issues. It would be quite useful to hook into vim's save mechanism. – Metagrapher Dec 9 '11 at 19:23
- Workflows in Git can be radically different from Subversion. You might never do this in svn because it would be abusive, but it has perfectly valid uses in git. For example a test integration server of some kind could be hooked to a test branch. Auto-commit on save then becomes quite useful. When you get to the point something is working, you would then use git rebase to squash all the activity into a single commit with a meaningful message, then merge into your devel branch. If this sounds scary or complex it's only because you are adapted to the limitations of a non-distributed source vcs. – Caleb Apr 4 '14 at 12:34.
Full details:
I want to report my take on this problem.
I have the
vimrc.vim in a git repository (
$VIMCONF, with a bootstrap .vimrc), and mapped
<Leader>ve ("vim edit") to open the vimrc in a new tab. This does not affect the current working directory.
So if I edit
vimrc.vim from a vim started in a different directory outside of my vim git repository, the approach which was discussed by Nick Edwards will fail. My hack is the following (for windows and linux)
if has('win32') " The git command has to be passed to git. So we need to call bash.exe. autocmd BufEnter *vimrc.vim silent! lcd %:p:h autocmd BufWritePost *vimrc.vim if expand('<amatch>') =~ substitute(expand('$VIMCONF\vimrc.vim'), '\', '/', "g") | execute '! git add % & git commit -m %' | endif else " Linux autocmd BufWritePost *vimrc.vim \ if expand('<amatch>') =~ expand($VIMCONF)."/vimrc.vim" | \ let tempdir = getcwd() | execute 'cd' fnameescape(expand('<amatch>:h')) | \ execute '! if git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m % ; fi' | \ execute 'cd' fnameescape(tempdir) | \ endif endif
Explanation:
- For windows, I change the directory to the vimrc file location. This is not the best, and I might change later to be similar to linux. Don't take this too serious but you might want to use it as a guide.
githas to be in the path (i.e. you should be able to type
git <ENTER>in
cmd.exe)
- For Linux, I first check that I am editing the right vimrc.vim
- Save the current working directory to
tempdir
- Change directory to the vimrc.vim file
- Do the git commit
- Return to the previous working directory
- I manually push the git changes
Notes:
- Commands can be "chained" in vim with
|. The chain will abort if any of the commands fails. To break over several lines start the next line with
\.
- This can be extended to ftplugin files. Your filters will be
*.vimand
expand($VIMCONF)."/ftplugin/"