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

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)
LVL 19
gelonidaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mihai BarbosTrying to tame bits. They're nasty.Commented:
Maybe
:autocmd BufWritePost * ! vim_save_command.sh <afile>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gelonidaAuthor Commented:
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.
0
Mihai BarbosTrying to tame bits. They're nasty.Commented:
: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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gelonidaAuthor Commented:
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.
0
Mihai BarbosTrying to tame bits. They're nasty.Commented:
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...
0
gelonidaAuthor Commented:
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:
#!/bin/bash

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

Open in new window

1
gelonidaAuthor Commented:
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.
0
Mihai BarbosTrying to tame bits. They're nasty.Commented:
Thanks. I think it's done somehow automatically now.
0
gelonidaAuthor Commented:
OK,  Thanks again. Just ping me in case you don't receive the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.