1

Is it possible for me to create a Vim command that will execute a function in my bash profile?

In my bash profile, I have a function that uploads a file via webdav to a remote location. The function uses duck.sh to accomplish this.

I would like to be able to edit a file in Vim, write updates to the file by typing :w + enter, and then run a custom command to execute my duck.sh function without closing the file.

Is this possible? Here's the function I would like to execute:

upload() {
        if [ -z "$1" ]; then
                echo "No file or directory submitted"
        else
                DIR=`pwd`
                if [ "$DIR" = "$WEBDAV_LOCAL_DIR" ]; then
                        duck -u $WEBDAV_USERNAME -p $WEBDAV_PASSWORD --upload $WEBDAV_URL/$1 $1 --existing overwrite
                else
                        echo "You must be in the following directory to use this function: $WEBDAV_LOCAL_DIR"
                fi
        fi
}

In order to work properly, the function will need to be passed the path to the file currently being edited.

1

1 Answer 1

1

You can use autocommands for this.

autocmd BufWrite .bash_profile silent! !bash -c upload

This will run every time a file called .bash_profile is written. You can remove the !silent if you don't want to silence the output. You can read about some of these commands in these help topics:

:help :autocmd
:help autocmd-events-abc
:help :silent
:help :!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.