Execute a command when vim exits

5,014

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

Share:
5,014

Related videos on Youtube

theillien
Author by

theillien

Sr. Systems Engineer in the federal contracting sector

Updated on September 18, 2022

Comments

  • theillien
    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
      Dominik Dosoudil over 6 years
      It'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
    Roland Smith over 10 years
    The fugitive plugin provides excellent git integration for vim.