3

I'm using this excellent script in my .zshrc file to accomplish zsh abbreviations: http://zshwiki.org/home/examples/zleiab

The above works great.

Here's my question:

How can I modify these abbreviations or the script to have the cursor end up in specific parts of the abbreviation instead of the end like so:

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

Should abbreviate to:

ack -C 5  [cursor]  --ignore-dir=.build
Jamis Charles
  • 5,827
  • 8
  • 32
  • 42

1 Answers1

2

Update the magic-abbrev-expand to

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
}

The abbreviation would be set as

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

Full script

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
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Gives me a parse error: `.zshrc:258: parse error near ']]'` from this line `[[ $abbreviations[$MATCH] ]] && RBUFFER=${LBUFFER[(ws:__CURSOR__:)2]}` – Jamis Charles Feb 21 '15 at 02:51
  • Please check if the parse error is from the first expression or the second. It is tested and works in the version of zsh I have. If it is second expression that is not supported in your zsh this solution isn't going to work at all. If is is first, we can modify and use different supported syntax. – amit_g Feb 21 '15 at 17:49
  • It is the first. When I remove the following the errors go away `[[ $abbreviations[$MATCH] ]]` – Jamis Charles Feb 22 '15 at 07:07
  • PS: I am running zsh via oh-my-zsh version `zsh 5.0.5 (x86_64-apple-darwin14.0)` – Jamis Charles Feb 22 '15 at 07:07
  • WOW. That's fantastic!!! Two final questions: 1) Can we have the cursor back one column? (-1) Currently the cursor ends up one space too far to the right. 2) What language is this (if I wanted to learn more about this)? Shell? Bash? Zsh? What scripting language? – Jamis Charles Feb 23 '15 at 18:56
  • Plus one more use case: `ack -i '_C_'-C 5 "` translates to `ack -i ' '-C 5` (the cursor is in the right place (on the closing '), but there is a space between the quotes. – Jamis Charles Feb 23 '15 at 18:59
  • Will gladly add another 50 bounty points if we can get these additional things sorted out – Jamis Charles Feb 23 '15 at 18:59
  • On further thought, I think I can live with those shortcomings. If you cant just point me in the right direction, I can do my own research to figure those things out. I don't know quite where to start though... – Jamis Charles Feb 23 '15 at 19:15
  • Updated. The extra space is removed so the abbreviation can control exactly where the cursor should be located. – amit_g Feb 23 '15 at 20:20
  • 1
    The full documentation is located at [zsh documentation](http://zsh.sourceforge.net/Doc/Release/zsh_toc.html). The most relevant parts that are used in this script are [parameter substitution](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion) and [Zsh-Line-Editor](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zsh-Line-Editor) – amit_g Feb 23 '15 at 20:23