Search code examples
shellzshzshrc

Avoid recursion in zsh command line


OK, I switched to zsh a few hours ago and I am in a huge config-fooling around spree, but this is likely a too big bite for me:

I'd like to to do a delimiter autopairnig, e.g. type ( and it inserts (). First fail:

bindkey -s "(" "()"

Second fail:

function autopair () {
zle -U "()"
zle backward-char
}
zle -N autopair autopair
bindkey "(" autopair

I think both fail because zsh tries to recurse on the "(" insert (-U seems to say this explicitly). Is there a way to avoid this? Substituting echo -ne "()" in the 1st line of the function of course avoids this but is uneditable... Any ideas how to force insert?

Bonus points (figuratively speaking:)): I'd like to set the cursor between the brackets after insertion. The zle backward-char in my function however doesn't do anything...

Just imagine how awesome this could be! Thanks!


Solution

  • function autopair()
    {
        LBUFFER+="("
        RBUFFER=")$RBUFFER"
    }
    zle -N autopair autopair
    bindkey "(" autopair