Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

You could use Vim's autocommands:

:autocmd BufWritePost * execute '!git add % && git commit -m %'

That's untested but it should add the file and commit it with the filename as commit message.
You want BufWritePost as this is triggered after the file is written. I'd imagine there are a lot of irksome or unsafe edge cases though.

share|improve this answer

You may want to look into the fugitive plugin, it's a very powerful git plugin for vim. You can also setup an autocmd as donothingsuccessfully suggests, but have it pop up :Gcommit or :Gstatus (from which you can cherry pick changes to add to the git index)

share|improve this answer

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), then uses git -a -m % so that only files that have previously been added are staged and committed.

autocmd BufWritePost * execute '! if [ -d .git ]; then git commit -a -m %; fi
share|improve this answer
There is missing single quote at the end of the cmd, right after fi. – Andrew-Dufresne Sep 25 '12 at 15:37
This 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

You surely could to this whit a macro or with command remapping.


I'm not sure it's a good option though. I'm not proficient in git for now and I know it's sometimes different from svn. But I wouldn't do this auto-commit with svn. Commit needs to be meaningful and not some save at regular time.

You normally commit because you added a function, fixed a bug, ... not just because you worked on a file. You won't end up with non compiling project or unfinished function.

share|improve this answer
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

Building on the answers others have provided:

autocmd BufWritePost * execute ':silent ! if git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m "Auto-commit: saved %"; fi > /dev/null 2>&1'

This is completely transparent. You won't get the "Press enter to continue" message (but if the commit didn't work, you won't know it failed).

share|improve this answer

I know this question is old and this is blatant self-promotion, but I wrote a Vim plugin that will trigger certain shell scripts to be run after specific Vim autocmd events. For example, a script named would be run (synchronously) every time the BufWritePost command is fired. You could then easily include whatever logic you want for performing the git commit inside that script. The plugin assumes specific naming conventions of these scripts and also supports "hook" scripts which trigger only on files matching certain names or having certain extensions.

Full details:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .