We help IT Professionals succeed at work.

run a (shell) script every vim saves a file.

gelonida asked
Last Modified: 2018-08-31
What's the easiest way to achieve following:

Whenever I save a file with vim:
- vim will check in the current working directory (the directory in which vim was saved) whether a script with a certain name exists. (e.g. vim_save_command.sh)
- if the file exists vim will execute it and pass as parameter the name of the saved file (which could be in a different directory)

This has to work under Linux. (even better if it also works under mingw / cygwin / windows, but not required)
Watch Question

Trying to tame bits. They're nasty.
This one is on us!
(Get your first solution completely free - no credit card required)


Thanks a lot
This seems to almost do the job. <afile> expands to the file name to be saved

but what happens if vim_save_command.sh does not exist in the current directory?

Can I avoid the error popping up in case the shell script does not exist?

This would allow me to add the autocmd to ~/.vimrc and add the 'magic post save command'  into the directory where I'd like to have the command and to have no action for all other directories.
Mihai BarbosTrying to tame bits. They're nasty.

:help filename-modifiers

Open in new window

might be of help.
I think that you can use another script at an absolute location that would check if vim_save... exists and then runs it.


Idea with global script is perfectly feasible in my case. Very good idea.

I added this global script detecting whether a local file exists or not and calling it.

Now just another question: Is there any way to get rid of the

"Press ENTER or type command to continue" prompt?

My script does in fact not create anything on stdout / stderr
It just kicks off some background tasks, that will report to another application.
Mihai BarbosTrying to tame bits. They're nasty.

You can try something like
:silent ....

Open in new window

but I haven't tested it (and I cannot do that right now)
It might also solve the problem with the error message when the script is not there...


Thanks a lot!!  everything's working now as expected.
In order to get rid of stdout I did following:
autocmd BufWritePost * silent exec "! ~/bin/vim_save_command.sh <afile>"

Open in new window

and the shell script looks like:

if [[ -x ./post_save.sh ]] ; then
    ./post_save.sh "$@"

Open in new window



Hmmm The web interface changed since I last asked a question.

I marked your answer as solution, but don't see where I can attribute points.
Mihai BarbosTrying to tame bits. They're nasty.

Thanks. I think it's done somehow automatically now.


OK,  Thanks again. Just ping me in case you don't receive the points.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.