gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:42 24 4
gpt4 key购买 nike

我是否可以创建一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com