junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
61.81k stars 2.35k forks source link

fix(zsh): use the '=~' operator instead of grep #3906

Closed LangLangBart closed 2 days ago

LangLangBart commented 2 days ago

description

Close #3904

Using Apple's default grep version and assigning the GREP_OPTIONS environment variable with --color=always, unintended ANSI escape sequences causing mischief in the correct retrieval of the history entry.

export GREP_OPTIONS="--color=always"
export PATH="/usr/bin:$PATH"
grep --version
# grep (BSD grep) 2.5.1-FreeBSD
source <(fzf --zsh)

Solution

  1. Use grep --color=never, or
  2. use the =~ operator coupled with MATCH to catch the matched string in case of a success.
man 1 zshmisc | less --pattern BASH_REMATCH
# string =~ regexp
#   true if string matches the regular expression regexp.  If the option RE_MATCH_PCRE is set regexp is tested as a PCRE regular expression
#   using the zsh/pcre module, else it is tested as a POSIX extended regular expression using the zsh/regex module.  Upon successful match,
#   some variables will be updated; no variables are changed if the matching fails.

#   If the option BASH_REMATCH is not set the scalar parameter MATCH is set to the substring that  matched  the  pattern  and  the  integer
#   parameters MBEGIN and MEND to the index of the start and end, respectively, of the match in string, such that if string is contained in
#   variable var the expression `${var[$MBEGIN,$MEND]}' is identical to `$MATCH'.  The setting  of  the  option  KSH_ARRAYS  is  respected.
#   Likewise,  the  array  match  is  set to the substrings that matched parenthesised subexpressions and the arrays mbegin and mend to the
#   indices of the start and end positions, respectively, of the substrings within string.  The arrays are not set if there were no  paren-
#   thesised subexpressions.  For example, if the string `a short string' is matched against the regular expression `s(...)t', then (assum-
#   ing the option KSH_ARRAYS is not set) MATCH, MBEGIN and MEND are `short', 3 and 7, respectively, while match, mbegin and mend are  sin-
#   gle entry arrays containing the strings `hor', `4' and `6', respectively.

#   If  the  option  BASH_REMATCH is set the array BASH_REMATCH is set to the substring that matched the pattern followed by the substrings
#   that matched parenthesised subexpressions within the pattern.
LangLangBart commented 2 days ago

Using Apple's default grep version and assigning the GREP_OPTIONS environment variable with --color=always, unintended ANSI escape sequences causing mischief in the correct retrieval of the history entry.

I expect this to be the reason for #3904, but it's probably good to wait until there is confirmation from the issue author.

junegunn commented 2 days ago

Thanks. We can't even trust grep no more, 😭