Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Error when using fzf-tab's preview feature #444

Open
NathanGrenier opened this issue May 18, 2024 · 10 comments
Open

[Q] Error when using fzf-tab's preview feature #444

NathanGrenier opened this issue May 18, 2024 · 10 comments
Labels
question Further information is requested

Comments

@NathanGrenier
Copy link

Describe your question

  • Windows 10
  • zsh
  • installed with zinit

Getting a weird error when using the preview feature in zsh. Not quite sure what to do to resolve it.

This is my .zshrc file:

# 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

# Set the directory we want to store zinit and plugins
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"

# Download Zinit, if it's not there yet
if [ ! -d "$ZINIT_HOME" ]; then
        mkdir -p "$(dirname $ZINIT_HOME)"
        git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
fi

# Source/Load zinit
source "${ZINIT_HOME}/zinit.zsh"

# Add Powerlevel10k
zinit ice depth=1; zinit light romkatv/powerlevel10k

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# Install zsh plugins
zinit light Aloxaf/fzf-tab
zinit light zsh-users/zsh-syntax-highlighting
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions

# Add in snippets
zinit snippet OMZP::git

# Load completions
autoload -U compinit && compinit

zinit cdreplay -q

# History
HISTSIZE=5000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
setopt appendhistory
setopt sharehistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups

# Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath'

# keybindings
bindkey '^f' autosuggest-accept
bindkey '^p' history-search-backward
bindkey '^n' history-search-forward

# Aliases
alias cl='clear'
alias ls='ls --color'

# Shell integrations
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
eval "$(zoxide init zsh)"

# Navigate to home directory
cd ~

Screenshot of the Error

image

@NathanGrenier NathanGrenier added the question Further information is requested label May 18, 2024
@NathanGrenier NathanGrenier changed the title Error when using fzf-tab's preview feature [Q] Error when using fzf-tab's preview feature May 18, 2024
@jathek
Copy link

jathek commented May 24, 2024

getting the same error. I'm wondering if the git-for-windows environment needs some adjustment from fzf-tab to work, because these dotfiles work fine on a linux box.

  • Microsoft Windows [Version 10.0.19045.4291]
  • zsh 5.9 (x86_64-pc-msys)
  • eza v0.18.16 (installed with winget install eza-community.eza -i)
  • antidote as plugin manager

Note: I'm trying to use eza, but ls also fails with the same error.

.zshrc
#!/bin/zsh
#
# .zshrc - Zsh file loaded on interactive shell sessions.
#

# 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

# set zsh options
setopt extended_glob

# clone antidote if necessary
[[ -d ${ZDOTDIR:-~}/.antidote ]] || git clone https://github.com/mattmc3/antidote ${ZDOTDIR:-~}/.antidote # https://github.com/getantidote/antidote is stable
# change antidote home directory - default on windows is %LocalAppData%/antidote
export ANTIDOTE_HOME=${ZDOTDIR:-~}/.antidote_home

# set the name of the static .zsh plugins file antidote will generate
zsh_plugins=${ZDOTDIR:-~}/.antidote.zsh_plugins.zsh
# ensure you have a $zsh_plugins_list file where you can add plugins
zsh_plugins_list=${zsh_plugins:r}_list.zsh
[[ -f ${zsh_plugins_list} ]] || touch ${zsh_plugins_list}
# source zstyles you might use with antidote
[[ -e ${ZDOTDIR:-~}/.antidote.zstyles.zsh ]] && source ${ZDOTDIR:-~}/.antidote.zstyles.zsh
# lazy-load antidote
fpath+=(${ZDOTDIR:-~}/.antidote)
autoload -Uz $fpath[-1]/antidote
# generate static file in a subshell when $zsh_plugins_list is updated
alias al="antidote bundle <${zsh_plugins_list} >|$zsh_plugins"
if [[ ! $zsh_plugins -nt ${zsh_plugins_list} ]]; then
  (al)
fi
# source your static plugins file
source $zsh_plugins

# autoload for prompts - see $zsh_plugins_list for explanation
export ZSH_THEME='powerlevel10k'
autoload -Uz promptinit && promptinit && prompt ${ZSH_THEME}

# to customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# source .profile
[[ ! -f ~/.profile ]] || . ~/.profile
.antidote.zsh_plugins_list.zsh
#!/bin/zsh
#
# .zsh_plugins.txt - antidote plugins file
#
# copied from https://github.com/getantidote/zdotdir/blob/1a34e6dd3d78862c82ba3babd19bd8c882c77d65/.zsh_plugins.txt


### Overview
#
# The `.zsh_plugins.txt` file is used by antidote to configure Zsh bundles. Bundles are
# just a fancy way of referring to Zsh utilities, frameworks, prompts, or plugins.
# `.zsh_plugins.txt` is used by antidote to generate a static `.zsh_plugins.zsh` file,
# which can then be sourced in your `.zshrc`. You can use this file as a starting point
# for your own config. Strip out instructional comments (ie: lines beginning with a '#')
# and any plugins you don't need, then add what you like and make it yours!
#
# NOTE: Order matters in this file! Some bundles need to be last, and others are
# prerequisites. Read the documentation for the plugins you use to ensure proper
# configuration.
#
###


### Completions
#
# You may want to add some additional completions to Zsh. Completions look in your fpath
# for completion functions, which are functions named with a leading underscore
# (eg: _git). You need to add all supplemental completions to your fpath prior to
# running `compinit` to use completions functionality properly. You will want to find
# a completion plugin that runs `compinit` for you, or you can run it yourself in
# your .zshrc after antidote loads like this:
#
#   autoload -Uz compinit && compinit
#
###
# zsh-users/zsh-completions is a popular plugin for adding supplemental completions.
# We combine the `path:` and `kind:fpath` annotations here:
jathek/zsh-completions path:src kind:fpath branch:custom

# With antidote, using Zephyr's completion plugin is recommended. This plugin is meant
# to be run near the TOP of your .zsh_plugins.txt file.
mattmc3/zephyr path:plugins/completion

# Most other compinit plugins should be near the END of .zsh_plugins.txt so that $fpath
# has been fully populated. One such plugin is zsh-utils and its completion subplugin.
# belak/zsh-utils path:completion


### Prompts
#
# You can make certain themes available to the built-in Zsh prompt command using the
# `kind:fpath` annotation. A theme is not active for the prompt until this command is
# run:
#
#   autoload -Uz promptinit && promptinit
#   prompt ${prompt_name:-pure}
#
# There are two ways to load prompts.
# - Add multiple prompt plugins to your `$fpath` with `kind:fpath` and then later in
#   your .zshrc run the Zsh built-in `prompt` to select the theme.
# - Or, just load one prompt like a regular plugin.
#
###
romkatv/powerlevel10k kind:fpath


### Frameworks
#
# Frameworks are a collection of Zsh features, settings, and plugins meant to ease
# Zsh configuration. Unfortunately, all that power can come with serious drawbacks.
# Frameworks can often set more settings or aliases than you need, cluttering your Zsh
# config and slowing things down. Use with caution!
#
# Frameworks are handled in antidote using the `path:location/of/plugin` annotation.
#
###

### Framework: Oh-My-Zsh
#
# Oh-My-Zsh is a very popular Zsh framework. You might choose to use plugins from OMZ
# in your own config. Depending on which plugins you use, you may also need to include
# OMZ's entire "lib" directory. If you do this, be careful - "lib" configures a lot of
# very broad settings. If you are familiar with what you are doing, you may choose to
# only include parts of "lib".
#
#   # use all of lib
#   ohmyzsh/ohmyzsh path:lib
#   # -OR-, only use only the specific files we care about
#   ohmyzsh/ohmyzsh path:lib/clipboard.zsh
#
####
ohmyzsh/ohmyzsh path:lib/key-bindings.zsh

# 🌬️ A Zsh framework as nice as a cool summer breeze
mattmc3/zephyr path:plugins/color
mattmc3/zephyr path:plugins/directory
# mattmc3/zephyr path:plugins/editor # use ohmyzsh for ctrl-del
mattmc3/zephyr path:plugins/environment
mattmc3/zephyr path:plugins/history
mattmc3/zephyr path:plugins/utility
mattmc3/zephyr path:plugins/zfunctions


### Regular Plugins
#
# Plugins are the simplest kind of bundles. You can point to a full git URL, or if you
# assume GitHub.com, then you can use the user/repo shorthand.
#
###
# rummik/zsh-tailf
# mattmc3/zman
# zle tweak for emulating `...'==`../..' etc.
yoogottamk/zsh-manydots-magic
# 📎 ZSH plugin that reminds you to use existing aliases for commands you just typed
MichaelAquilina/zsh-you-should-use path:you-should-use.plugin.zsh
# Good-lookin' diffs. Actually… nah… The best-lookin' diffs. 🎉
so-fancy/diff-so-fancy
# Replace zsh's default completion selection menu with fzf!
Aloxaf/fzf-tab
# A ZSH plugin to set the terminal tab title according to current directory or running process. It's based (forked) from Oh-My-ZSH one.
trystan2k/zsh-tab-title


### widget wrapping plugins
# zsh-users/zsh-syntax-highlighting # use zdharma-continuum/fast-syntax-highlighting instead
zdharma-continuum/fast-syntax-highlighting


### Final Plugins
#
# Remember plugin order matters! Put plugins that need run last at the bottom of your
# .zsh_plugins.txt file.
#
###
# These popular core plugins should be loaded at the end
zsh-users/zsh-autosuggestions
zsh-users/zsh-history-substring-search
.antidote.zstyles.zsh
#!/bin/zsh
#
# .zstyles - Set zstyle settings for plugins that need them.
#

#
# Antidote
#

# friendly plugin directories - no 'https-COLON--SLASH--SLASH-github.com-SLASH-'
zstyle ':antidote:bundle' use-friendly-names 'yes'
# change default zsh_plugins.txt name
zstyle ':antidote:bundle' file ${zsh_plugins_list}

# Aloxaf/fzf-tab
# 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 '<' '>'

@Aloxaf
Copy link
Owner

Aloxaf commented Jun 11, 2024

what's the value of $ZSH_NAME on windows?

@jathek
Copy link

jathek commented Jun 12, 2024

just "zsh"

image

@srouquette
Copy link

srouquette commented Aug 18, 2024

I kinda have the same problem, but it's most likely related to msys2.
If I run this simple command, I have the preview: fzf --preview='less {}'
But if I run with fzf-tab, I have this error:

image

(it tries to run zsh as if it was in the current dir)

If I fix the path to zsh, I have this new error:

image

It seems related to path resolution with the temp dir ?

To fix the path, I only added this in -ftb-fzf:

shell_cmd=$(which $ZSH_NAME)
SHELL=$shell_cmd $fzf_command \

@jathek
Copy link

jathek commented Aug 19, 2024

I tried your zsh path fix and get the same (new) error as you. I agree there's a second issue with path resolution. $TEMP on Windows (with msys2 or git-for-windows) is /tmp, but behind the scenes it is actually a Windows path of C:\Users\USERNAME\AppData\Local\Temp. The Windows path is being called by zsh at some point (asked by fzf?) and the \ are being interpreted as escape characters, so the path becomes invalid.

I did check my temp folder, and a folder is actually being created in the temp folder and populated with files by fzf-tab.
image

However, the /tmp/fzf-temp-123456789 temp files (from fzf?) aren't created anymore because of the path issue. If I remove the ZSH path fix, the /tmp/fzf-temp-123456789 files get created, but the zsh "file does not exist" error returns.

(the above folder from fzf-tab only gets created properly after the following change in -ftb-fzf. Without it, the folder is "zsh-fzf-tab-" instead of "zsh-fzf-tab-USERNAME")

- local tmp_dir=${TMPPREFIX:-/tmp/zsh}-fzf-tab-$USER
+ local tmp_dir=${TMPPREFIX:-/tmp/zsh}-fzf-tab-$(whoami)

@srouquette
Copy link

srouquette commented Aug 19, 2024

I checked fzf code:

func WriteTemporaryFile(data []string, printSep string) string {
	f, err := os.CreateTemp("", "fzf-temp-*")
	if err != nil {
		// Unable to create temporary file
		// FIXME: Should we terminate the program?
		return ""
	}

🤣

as for msys2, they may rewrite some code during compilation to make it work. For example, git-for-windows only works in MINGW64, they didn't port it to UCRT64 or CLANG64.

I check how fzf was compiled and they didn't do anything special. but still, fzf works fine in other cases.

now... try this on your command line: zsh $(which fzf)

it doesn't work anymore, but I'm not sure if it should work in the first place...

@xd003
Copy link

xd003 commented Aug 28, 2024

Can confirm that its not working on windows msys2 zsh, works fine for me on linux
Although i don't get any error as in the screenshot posted by the OP, previews just don't show up

@sylvain-rouquette
Copy link

I can have the preview if I'm using fzf + other app. The problem seems to come from fzf-tab.

my zdotdir. I have a preview with CTRL+T and ALT+C.

@xd003
Copy link

xd003 commented Aug 28, 2024

I can have the preview if I'm using fzf + other app. The problem seems to come from fzf-tab.

Just curious what is the alternative that you are talking about

@srouquette
Copy link

srouquette commented Aug 28, 2024

the default keybindings are already quite good. I added preview on CTRL+T and ALT+C. You can also autocomplete cd with
cd **<TAB>. I'm just missing the completion for command line parameters.

I'm using the OMZ plugin, and how I customized it with fd, eza and rg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants