3

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!

user673592
  • 2,090
  • 1
  • 22
  • 37

1 Answers1

6
function autopair()
{
    LBUFFER+="("
    RBUFFER=")$RBUFFER"
}
zle -N autopair autopair
bindkey "(" autopair
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Thank you, but it does not work for me... No errors, just does not insert anything after "(". I've only copied your snippet to my .zshrc (and sourced it) - or do I have to also somehow call it? – user673592 Jul 13 '11 at 10:26
  • @user673592 *Try replacing your function*. I have not written all this zle stuff because it already was in your question. – ZyX Jul 13 '11 at 13:28
  • Updated so that solution is obvious (copied that two lines from your question). – ZyX Jul 13 '11 at 13:58
  • 2
    My gosh I can be such an idiot! (To my defense I was in a terrible rush earlier today...). Yes, this works and is awesome beyond measure! Thanks so much! – user673592 Jul 13 '11 at 17:34