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?
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.
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'
You may want to look into the fugitive plugin, it's a very powerful git plugin for vim. You can also setup an
donothingsuccessfully suggests, but have it pop up
:Gstatus (from which you can cherry pick changes to add to the git index)
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).
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.
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
.bufwritepost.vimhook.sh 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: https://github.com/ahw/vim-hooks
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
- 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
- For Linux, I first check that I am editing the right vimrc.vim
- Save the current working directory to
- Change directory to the vimrc.vim file
- Do the git commit
- Return to the previous working directory
- I manually push the git changes
- 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