Aloxaf / fzf-tab

Replace zsh's default completion selection menu with fzf!
MIT License
3.37k stars 97 forks source link

[BUG] Preview always shows "fork/exec /usr/bin/zsh: invalid argument" #461

Closed LoveCatc closed 1 month ago

LoveCatc commented 3 months ago

Describe the bug

The preview window shows "fork/exec /usr/bin/zsh: invalid argument" and it even happens when opening logs (C-x .).

I can make sure:

To Reproduce

Steps to reproduce the behavior:

  1. Type ls
  2. Press Tab
  3. See error

Expected behavior

The preview window should give correct information instead of an error.

Screenshots

image

Environment:

Minimal zshrc

.zshrc

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

if [[ ! -f ~/.zinit/bin/zinit.zsh ]]; then
    mkdir ~/.zinit
    git clone https://github.com/zdharma-continuum/zinit.git ~/.zinit/bin
fi

# load zinit
source ~/.zinit/bin/zinit.zsh

# 加载 OMZ 框架及部分插件
zinit snippet OMZ::lib/completion.zsh
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZ::lib/key-bindings.zsh
zinit snippet OMZ::lib/theme-and-appearance.zsh
zinit snippet OMZ::lib/directories.zsh
zinit snippet OMZ::plugins/colored-man-pages/colored-man-pages.plugin.zsh
zinit snippet OMZ::plugins/sudo/sudo.plugin.zsh
zinit snippet OMZ::plugins/mvn/mvn.plugin.zsh
zinit snippet OMZ::plugins/tmux/tmux.plugin.zsh
zinit snippet OMZ::plugins/tmuxinator/tmuxinator.plugin.zsh
zinit snippet OMZ::plugins/command-not-found/command-not-found.plugin.zsh
zinit snippet OMZ::plugins/pip/pip.plugin.zsh

# zinit light denysdovhan/spaceship-prompt
zinit ice depth=1; zinit light romkatv/powerlevel10k

autoload -Uz compinit

# binary release, unpack provide fzf
zinit ice from"gh-r" as"program"
zinit light junegunn/fzf
zinit light Aloxaf/fzf-tab

# 语法高亮
zinit ice lucid wait='0' atinit='zpcompinit'
zinit light zdharma-continuum/fast-syntax-highlighting

# 自动建议
zinit ice lucid wait="0" atload='_zsh_autosuggest_start'
zinit light zsh-users/zsh-autosuggestions

# 补全
zinit ice lucid wait='0'
zinit light zsh-users/zsh-completions

source $HOME/dotfiles/zsh/fzf.zsh

[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

fzf.zsh

export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git --exclude .idea ""'

export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --preview '(highlight -O ansi {} || cat {}) 2> /dev/null | head -500'"

# disable sort when completing `git checkout`
zstyle ':completion:*:git-checkout:*' sort false
# set descriptions format to enable group support
# NOTE: don't use escape sequences here, fzf-tab will ignore them
zstyle ':completion:*:descriptions' format '[%d]'
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# force zsh not to show completion menu, which allows fzf-tab to capture the unambiguous prefix
zstyle ':completion:*' menu no
# preview directory's content with eza when completing cd
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath'
# switch group using `<` and `>`
zstyle ':fzf-tab:*' switch-group '<' '>'

Log

If applicable, use C-x . to trigger completion and provide the log.

If there are only three lines in your log, please make sure your fzf-tab is loaded with the correct order (see the checklist above).

no logs are shown. image

NightWatcher314 commented 2 months ago

I have the same issue with you.

I reduce my config file step by step, and find the problem in line zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath' the command eza doesn't exsit on my computer. After I run sudo apt install eza, the problem fixed.

LoveCatc commented 2 months ago

Hi NightWatcher314, thank you for the sincere help! Unfortunately, this does not solve my problem. But you provide a very nice point, and I should look through the config more carefully. Anyway thank you again!

lsalazarm99 commented 2 months ago

I have the same problem. It's related to the FZF_DEFAULT_OPTS variable, specifically the "--preview" argument.

I know that's a fzf thing, but the issue only happens when I use fzf through this plugin. If I simply run fzf, the preview window works fine.

lsalazarm99 commented 2 months ago

Also, if I use the fzf plugin (for example, writing nano ** and pressing Tab) it also works fine.

LoveCatc commented 1 month ago

Hi, thank you guys for helping me a lot! Today I carefully looked through the history issues and found #77 is really similar to my case. With the help of GPT-4, I managed to find the working config for me. Besides the basic FZF_DEFAULT_COMMAND and FZF_DEFAULT_OPTS variables, we have to also use zstyle to make things work properly. I add the following part to make it work:

export _PREVIEW_="$HOME/dotfiles/zsh/preview.zsh"
local extract="
local in=\${\${\"\$(<{f})\"%\$'\0'*}#*\$'\0'}
local -A ctxt
for entry in \${(@ps:\2:)CTXT}; do
    local key=\${entry%%=*}
    local value=\${entry#*=}
    ctxt[\$key]=\$value
done
local realpath=\${ctxt[IPREFIX]}\${ctxt[hpre]}\$in
realpath=\${(Qe)~realpath}
"
zstyle ':fzf-tab:complete:*:*' fzf-flags --preview=$extract';$_PREVIEW_ $realpath'

And the preview.zsh is very simple:

#!/usr/bin/env zsh

# Check if the file exists
if [[ -e "$1" ]]; then
    # If the file is a regular text file, display its content
    if [[ $(file --mime-type -b "$1") == text/* ]]; then
        # Use highlight for syntax highlighting or fallback to cat
        (highlight -O ansi "$1" || cat "$1") 2> /dev/null | head -500
    else
        # If not a text file, display the file type
        file "$1"
    fi
else
    # If the file does not exist, print the filename
    echo "$1"
fi

Hope this would help!