如何像vim的ctrlp插件一样在bash中模糊完整的文件名?

fqs*_*sxr 5 bash autocomplete

说我的密码在~/myproject/,我有一个文件~/myproject/scripts/com/example/module/run_main_script.sh

在带有ctrlp 插件的vim 中,我可以按Ctrl+ P,输入run_main_ Enter,我正在编辑该脚本。

我想在 bash 中运行该脚本(带有一些参数)。而且我不想输入完整路径。有没有办法在 bash 中做到这一点?

Bob*_*bby 1

这就是PATH变量通常的用途。不过,我不会将您的整个主目录添加到您的PATH. 考虑添加一个专用目录(例如~/bin)以将可执行文件添加到您的路径中。

但是,您可以向您添加一个函数~/.bashrc,该函数允许您搜索并运行脚本......如下所示:

# brun stands for "blindly run"
function brun {
    # Find the desired script and store
    # store the results in an array.
    results=(
        $(find ~/ -type f -name "$1")
    )

    if [ ${#results[@]} -eq 0 ]; then   # Nothing was found
        echo "Could not find: $1"
        return 1

    elif [ ${#results[@]} -eq 1 ]; then   # Exactly one file was found
        target=${results[0]}

        echo "Found: $target"

        if [ -x  "$target" ]; then   # Check if it is executable
            # Hand over control to the target script.
            # In this case we use exec because we wanted
            # the found script anyway.
            exec "$target" ${@:2}
        else
            echo "Target is not executable!"
            return 1
        fi

    elif [ ${#results[@]} -gt 1 ]; then   # There are many!
        echo "Found multiple candidates:"
        for item in "${results[@]}"; do
            echo $item
        done
        return 1
    fi
}
Run Code Online (Sandbox Code Playgroud)