Execute a command when vim exits
Solution 1
Though it is not a good idea to commit changes immediately after editing and quitting the file, you can try vim auto commands.
augroup autocom
autocmd!
"executes the command on quit
autocmd VimLeave *.cpp !your_command
"execute the command on write
autocmd BufWritePost,FileWritePost *.cpp !your_commad
augroup END
Solution 2
Auto-commit on exit sounds like a bad idea. You're essentially encouraging your own bad practices.
To commit from within vim you'd do it the same way you execute any other command:
:!git commit -m "look ma I remembered to commit" my_dir/my_file.c
After running:
:w
to save it ofc
Related videos on Youtube
theillien
Sr. Systems Engineer in the federal contracting sector
Updated on September 18, 2022Comments
-
theillien over 1 year
Is there a configuration I can add to a .vimrc file which will cause vim to run a command when a file is closed?
For instance, suppose I'm editing a file that is under version control. Once I'm done editing I would like a command to commit my changes to be run immediately before vim fully closes without having to run it separately after saving/quiting the file.
I'm curious about this as I have a tendency to forget to commit my changes.
Alternatively and if it is not possible to do what I'm asking, is it possible to configure an internal vim :command that will run the commit from with vim before I close the file?
-
Dominik Dosoudil over 6 yearsIt's not in vimrc however you could use shell &&. I use it like this: vim a.c && gcc a.c -Wall -Werror -pedantic && ./a.out which opens file, compiles after closing and runs it. You can create a shell script to simplify it.
-
-
Roland Smith over 10 yearsThe fugitive plugin provides excellent git integration for vim.