Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

5 Answers

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
add comment

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
1  
There is missing single quote at the end of the cmd, right after fi. –  Andrew-Dufresne Sep 25 '12 at 15:37
2  
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
add comment

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
add comment

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
add comment

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

(Check http://stackoverflow.com/questions/117150/can-i-re-map-commands-in-vim)

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 at 12:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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