junegunn / fzf

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

docs: update zsh integration instructions #3794

Closed LangLangBart closed 4 months ago

LangLangBart commented 4 months ago

Problem

In #3736 the alias expansion was only tested for the old way of sourcing the *.zsh shell files.

export FZF_DEFAULT_OPTS=""
alias cat='bat --color=always --number'
source <path to file>
kill ** <TAB>

However, when attempting to use eval, it doesn't disable alias expansion, leading to the same issue as seen in #3731.

export FZF_DEFAULT_OPTS=""
alias cat='bat --color=always --number'
eval "$(fzf --zsh)"
kill ** <TAB>

Proposal

The eval command affects the setup established in issue #1944. However, the following syntax appears to work effectively:

source <(fzf --zsh)

Alternative Solution

To resolve this, we could prepend command to the commands, such as awk, cat, sed, etc.

--- a/shell/completion.zsh
+++ b/shell/completion.zsh
@@ -201,5 +201,5 @@ _fzf_feed_fifo() (
   command rm -f "$1"
   mkfifo "$1"
-  cat <&0 > "$1" &
+  command cat <&0 > "$1" &
 )

Other Projects

For comparison, consider how kubectl is set up on macOS:


Pinging @romkatv for your valuable input: we're trying to understand why eval "$(fzf --zsh)" has issues with alias expansion, while source <(fzf --zsh) seems to work well. Could you help us see if there are any potential problems with using the latter for sourcing the *.zsh files? Your expertise would be incredibly helpful.

junegunn commented 4 months ago

Thanks, I can confirm thatsource does fix the issue though I don't understand why.

romkatv commented 4 months ago

Aliases are expanded during parsing. When using eval, the whole script is parsed at once, hence aliases are expanded before anything from $script is executed. When using source, commands are parsed one at a time.

Here's a demo:

% eval '
  alias greet="echo hello"
  greet'
(eval):3: command not found: greet

% source <(print '
  alias greet="echo hello"
  greet')
hello
LangLangBart commented 4 months ago

Thank you for the explanation.

alias cat='bat --color=always --number'
eval "$(fzf --zsh)"
whence -c _fzf_feed_fifo
_fzf_feed_fifo () {
  (
    command rm -f "$1"
    mkfifo "$1"
    bat --color=always --number <&0 > "$1" &
  )
}
alias cat='bat --color=always --number'
source <(fzf --zsh)
whence -c _fzf_feed_fifo
_fzf_feed_fifo () {
  (
    command rm -f "$1"
    mkfifo "$1"
    cat <&0 > "$1" &
  )
}