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

标签 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
}

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

最佳答案

您可以为此使用自动命令。

autocmd BufWrite .bash_profile silent! !bash -c upload

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

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

关于linux - 创建自定义 Vim 命令以在 bash 配置文件中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601883/

相关文章:

linux -/usr/bin/ts : line 3: =head1: command not found

php - 特使 mkdir 问题

linux - 我如何为在 linux 中的文件中搜索文本创建别名

linux - 无法让我的 if 在我的 shellscript 中工作

linux - 用于查找子字符串出现次数的 Shell 脚本

linux - 在 bash 脚本中使用 "alternate screen"

bash - BASH 脚本中的“换行意外”错误

python - 从 python 脚本导入 shell 变量

shell - 如何在unix中使用进程ID仅获取进程名称

arrays - 我可以设置多少个参数