如何在Zsh缩写后移动光标?

时间:2015-02-17 23:21:29

标签: bash zsh zshrc oh-my-zsh zsh-completion

我在.zshrc文件中使用这个优秀的脚本来完成zsh缩写: http://zshwiki.org/home/examples/zleiab

以上作品很棒。

这是我的问题:

如何修改这些缩写或脚本以使光标最终位于缩写的特定部分而不是结尾,如下所示:

 "ac"    "ack -C 5 {cursorHere} --ignore-dir=.build"¬

应缩写为:

ack -C 5  [cursor]  --ignore-dir=.build

1 个答案:

答案 0 :(得分:2)

将magic-abbrev-expand更新为

magic-abbrev-expand() {
    local MATCH
    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
    command=${abbreviations[$MATCH]}
    LBUFFER+=${command:-$MATCH}

    if [[ "${command}" =~ "__CURSOR__" ]]
    then
        RBUFFER=${LBUFFER[(ws:__CURSOR__:)2]}
        LBUFFER=${LBUFFER[(ws:__CURSOR__:)1]}
    else
        zle self-insert
    fi
}

缩写将设置为

"ac"    "ack -C 5__CURSOR__--ignore-dir=.build"
"Ii"    "Hello__CURSOR__! How are you"

完整脚本

setopt extendedglob
typeset -Ag abbreviations
abbreviations=(
  "ac"    "ack -C 5__CURSOR__--ignore-dir=.build"
  "Ii"    "Hello__CURSOR__! How are you"
  "Im"    "| more"
  "Ia"    "| awk"
  "Ig"    "| grep"
  "Ieg"   "| egrep"
  "Iag"   "| agrep"
  "Igr"   "| groff -s -p -t -e -Tlatin1 -mandoc"
  "Ip"    "| $PAGER"
  "Ih"    "| head"
  "Ik"    "| keep"
  "It"    "| tail"
  "Is"    "| sort"
  "Iv"    "| ${VISUAL:-${EDITOR}}"
  "Iw"    "| wc"
  "Ix"    "| xargs"
)

magic-abbrev-expand() {
    local MATCH
    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
    command=${abbreviations[$MATCH]}
    LBUFFER+=${command:-$MATCH}

    if [[ "${command}" =~ "__CURSOR__" ]]
    then
        RBUFFER=${LBUFFER[(ws:__CURSOR__:)2]}
        LBUFFER=${LBUFFER[(ws:__CURSOR__:)1]}
    else
        zle self-insert
    fi
}

no-magic-abbrev-expand() {
  LBUFFER+=' '
}

zle -N magic-abbrev-expand
zle -N no-magic-abbrev-expand
bindkey " " magic-abbrev-expand
bindkey "^x " no-magic-abbrev-expand
bindkey -M isearch " " self-insert