zsh bindkey在函数中不起作用

时间:2016-11-03 11:07:13

标签: shell zsh zshrc

我为.zshrc编写了一个小函数来加载插件并设置其键绑定

function loadPlugin() {
    # Function to load external zsh plugins and set keybindings.
    pluginName=$1
    pluginPath=$2
    if [ -r $pluginPath ];then
    source $pluginPath
    else
    echo "$pluginName plugin can not be found at: $pluginPath"
    fi

    # set keybindings
    shift
    shift

    while [[ $# > 0 ]]; do
    bindkey -M emacs '$1' '$2'
    shift
    shift
    done
}

加载部分有效,但没有设置键绑定:

loadPlugin "History search" \
       "$HOME/zsh.d/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh" \
       '^p' "history-substring-search-up" \
       '^n' "history-substring-search-down"

没有错误输出,如果我在使用相同的参数调用它之后调用函数外部的绑定命令,它们将起作用。

bindkey -M emacs '^p' "history-substring-search-up"
bindkey -M emacs '^n' "history-substring-search-down"

1 个答案:

答案 0 :(得分:1)

您的单引号会阻止展开$1$2。改为使用双引号。

bindkey -M emacs "$1" "$2"