flyingL123
flyingL123

Reputation: 8046

Create custom Vim command to run function in bash profile

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.

Upvotes: 1

Views: 422

Answers (1)

EvergreenTree
EvergreenTree

Reputation: 2058

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 :!

Upvotes: 1

Related Questions