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.

flyingL123
  • 7,686
  • 11
  • 66
  • 135
  • possible duplicate of [Executing Bash functions from within Vim - how can I do it?](http://stackoverflow.com/questions/6162809/executing-bash-functions-from-within-vim-how-can-i-do-it) – hek2mgl Feb 19 '15 at 09:16

1 Answers1

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 :!
EvergreenTree
  • 1,788
  • 2
  • 16
  • 30