创建自定义Vim命令以在bash配置文件中运行函数

时间:2015-02-19 08:46:14

标签: linux bash shell vim

我是否可以创建一个Vim命令来执行我的bash配置文件中的功能?

在我的bash配置文件中,我有一个函数可以通过webdav将文件上传到远程位置。该函数使用duck.sh来完成此任务。

我希望能够在Vim中编辑文件,通过键入:w + enter来编写文件更新,然后运行自定义命令来执行我的duck.sh函数而不关闭文件。

这可能吗?这是我想要执行的功能:

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
}

为了正常工作,需要将该函数传递给当前正在编辑的文件的路径。

1 个答案:

答案 0 :(得分:1)

您可以使用自动命令。

autocmd BufWrite .bash_profile silent! !bash -c upload

每次写入名为.bash_profile的文件时都会运行。如果您不想让输出静音,可以删除!silent。您可以在以下帮助主题中阅读其中一些命令:

:help :autocmd
:help autocmd-events-abc
:help :silent
:help :!