Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid recursion in zsh command line

Tags:

shell

zsh

zshrc

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!

like image 216
user673592 Avatar asked Feb 23 '26 21:02

user673592


1 Answers

function autopair()
{
    LBUFFER+="("
    RBUFFER=")$RBUFFER"
}
zle -N autopair autopair
bindkey "(" autopair
like image 120
ZyX Avatar answered Feb 25 '26 21:02

ZyX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!