简体   繁体   English

zsh bindkey 在函数中不起作用

[英]zsh bindkey does not work in a function

I wrote a small function for .zshrc to load plugins and set their keybindings我为 .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
}

The loading part works, but it does not set the keybindings:加载部分工作,但它不设置键绑定:

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"

There is no error output, and if I called the binding commands outside the function after calling it with the same arguments they would work.没有错误输出,如果我在使用相同的参数调用函数后调用绑定命令,它们将起作用。

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

Your single quotes prevent $1 and $2 from being expanded.您的单引号防止$1$2被扩展。 Use double-quotes instead.改用双引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM