What does `LBUFFER%%(#m)[_a-zA-Z0-9]#` do in zsh?

I need help understanding what the following code in this documentation means.

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

I learned that LBUFFER contains the left from the cursor. However, there are 3 things that confuse me.

  1. What is the %% doing? Is it escaping %?
  2. What does (#m)[_a-zA-Z0-9]# do? Is it something like m/[_a-zA-Z0-9]/ in Perl? If so, what is done to the matched string?
  3. What is the :- part doing in the second line?

Thanks.

1 Answer

  1. ${a%%b} removes the longest occurrence of b (which can be a regular expression), from a.

  2. [_a-zA-Z0-9]# is a regular expression matching an alphanumeric character (as indicated by the stuff between []) zero or more times (specified by the # glob operator)

  3. (#m) populates the $MATCH variable after evaluating the previous regular expression.

  4. The combination of 1, 2, and 3 means that the stretch of alphanumeric characters at the end of the $LBUFFER is deleted and stored in $MATCH.

  5. ${a:-b} makes a equal to b only if a is not defined.

If you want to learn more, take a look at the expansion chapter of the zsh manual.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like