Aloxaf / fzf-tab

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

Trouble getting fzf-tab to work without nvm [BUG] #398

Closed Dyphen closed 11 months ago

Dyphen commented 11 months ago

Describe the bug

fzf-tab stops working when I stop sourcing nvm in my .zshrc. I switched over to using another node version manager today (rtx), and while that was working fine, I noticed that fzf-tab wasn't working anymore. Getting errors like shown below.

❯ cd _ftb__main_complete:53: command not found: _setup
❯ cd _ftb__main_complete:143: command not found: _complete
_ftb__main_complete:143: command not found: _ignored
❯ cd _ftb__main_complete:143: command not found: _complete
_ftb__main_complete:143: command not found: _complete
_ftb__main_complete:143: command not found: _complete
_ftb__main_complete:143: command not found: _complete
_ftb__main_complete:143: command not found: _ignored
_ftb__main_complete:143: command not found: _ignored
_ftb__main_complete:143: command not found: _ignored
_ftb__main_complete:143: command not found: _ignored

I reinstalled fzf, fzf-tab, then created a minimal .zshrc setup (pasted below) for troubleshooting purposes and realized that when I commented out the three nvm lines in my .zshrc it broke, and when I source it back in, it works. Since my node was working fine being managed by rtx, I figured maybe it has something to do with not sourcing bash_completion, but when I did that manually it still did not work. Any ideas?

[Edit]: Tried using fzf-tab-completion and also ran into same issue, so guessing this has something to do with fzf/node/completion sourcing rather than fzf-tab specifically?

I can make sure:

To Reproduce

Steps to reproduce the behavior:

  1. Comment out nvm sourcing lines in .zshrc
  2. Open new terminal session
  3. Write any letter or command and press tab to enter fzf-tab (e.g. cd <tab>)
  4. Receive error

Expected behavior

fzf-tab should work normally?

Screenshots

image

Environment:

Minimal zshrc

export PATH=/opt/homebrew/bin:$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin

# commenting out these lines out breaks fzf-tab
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

eval "$(rtx activate zsh)"

source ~/.config/zsh/plugins/fzf-tab/fzf-tab.zsh

Log

N/A

Dyphen commented 11 months ago

I was able to rectify this by manually adding compinit (autoload -Uz compinit && compinit).

wiraki commented 6 months ago

Hello, I have the same issue. Without those nvm lines fzf-tab won't work. Even with the autoload -Uz compinit && compinit line.

My .zshrc:

# zmodload zsh/zprof

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.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

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

# Load completions
autoload -Uz compinit && compinit

# For better performance: (check zinit documentation)
zinit cdreplay -q

# Enable completion descriptions to show the types
zstyle ':completion:*:descriptions' format '#%d'
# Custom commands
backward-kill-dir() {
    local WORDCHARS=${WORDCHARS/\//}
    zle backward-kill-word
    zle -f kill # Ensures that after repeated backward-kill-dir, Ctrl+Y will restore all of them.
}
zle -N backward-kill-dir

my-backward-kill-word() {
    # Add colon, comma, single/double quotes to word chars
    local WORDCHARS='*?_-.[]~=/&;!#$%^(){}<>:,"'"'"
    zle -f kill # Append to the kill ring on subsequent kills.
    zle backward-kill-word
}
zle -N my-backward-kill-word

# Custom keybinds
bindkey "\C-k" vi-kill-eol
bindkey '^f' backward-kill-dir
bindkey '^w' my-backward-kill-word
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word

########### Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
# Enable completion descriptions to show the types
zstyle ':completion:*:descriptions' format '#%d'
# disable default zsh completion menu
zstyle ':completion:*' menu no
# Group the completions by type
zstyle ':completion:*' group-name ''
# Customize fzf-tab completions
zstyle ':fzf-tab:*' show-group full
# Change keybinding to switch groups
zstyle ':fzf-tab:*' switch-group ctrl-h ctrl-l
# Show type even when only one group
zstyle ':fzf-tab:*' single-group color header
# Add directory preview when completing cd or ls etc.
zstyle ':fzf-tab:complete:(cd|ls|ll|lsd|lsdd|j|eza):*' fzf-preview '[[ -d $realpath ]] && eza -1 --color=always $realpath'
# Increase fzf prompt size
zstyle ':fzf-tab:*' fzf-pad 5
zstyle ':fzf-tab:*' fzf-min-height 20
# Accept input as result when ctrl-c
zstyle ':fzf-tab:*' print-query ctrl-c
# Accept selected entry on enter (disabled for now)
# zstyle ':fzf-tab:*' accept-line enter
# Add file content preview for relevant commands
zstyle ':fzf-tab:complete:((micro|cut|cp|mv|rm|bat|less|code|nano|atom|vd|nvim|kvim|zvim):argument-rest|kate:*)' fzf-preview 'bat --color=always -- $realpath 2>/dev/null || ls --color=always -- $realpath'

# Setup pyenv
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"

# Shell integrations
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
eval "$(fzf --zsh)"
eval "$(zoxide init --cmd cd zsh)"
eval "$(navi widget zsh)"
eval "$(atuin init zsh)"
eval "$(pyenv init -)"

# Configure command used for **<TAB> completion
_fzf_compgen_path() {
  fd --no-ignore-vcs --hidden --follow --exclude ".git" --exclude "conf" . "$1"
}

# # nvm configuration
# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"                   # This loads nvm

# Shell wrapper that provides the ability to change the current working directory when exiting Yazi
function yy() {
    local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
    yazi "$@" --cwd-file="$tmp"
    if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
        cd "$cwd"
    fi
    rm -f -- "$tmp"
}

# Override history size limits
export HISTSIZE=1000000000
export SAVEHIST=$HISTSIZE

# Source kitty_keys to print keybinds
# Now "kitty_keys" command is available
[[ ! -f ~/software/kitty_keys/kitty_keys.sh ]] || source ~/software/kitty_keys/kitty_keys.sh

# Load my aliases
[[ ! -f ~/.config/zsh/my-aliases.zsh ]] || source ~/.config/zsh/my-aliases.zsh

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

# zprof

My .zshenv:

# Add at the beginning of PATH
typeset -U PATH path
path=(~/.local/bin ~/bin ~/.cargo/bin /usr/local/bin $path)

# Add Homebrew (linuxbrew) site-functions to fpath (to have zsh-completions for tools installed using brew)
fpath=($HOMEBREW_PREFIX/share/zsh/site-functions $fpath)

# Export PATH
export PATH

# fzf configuration
if type rg &> /dev/null; then
  export FZF_DEFAULT_COMMAND='fd --no-ignore-vcs --hidden --exclude ".git" --exclude "conf"'
  export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
  export FZF_DEFAULT_OPTS="-m --height 70% --layout=reverse --border"
fi

# Export variables for BAT
## export BAT_THEME="Nord"
## export BAT_PAGER="less"

# Export spicetify
export SPICETIFY_INSTALL="$HOME/.spicetify"
export PATH="$PATH:$HOME/.spicetify"

# Set editor
export EDITOR="nvim -u ~/.config/nvim-kick/init.lua"

# Stop telemetry from microsoft dotnet
export DOTNET_CLI_TELEMETRY_OPTOUT="true"

# nvm configuration
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"                   # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
ghry5 commented 5 months ago

@wiraki I ran into the same issue today. I was able to fix it by removing eval "$(fzf --zsh)" from my .zshrc

wiraki commented 5 months ago

@wiraki I ran into the same issue today. I was able to fix it by removing eval "$(fzf --zsh)" from my .zshrc

@ghry5 Thanks for commenting! Isn't that necessary for fzf shell integration? I do use a feature related to that, which is **<Tab> quite often, so I am not sure I can, or want to turn that off? Let me know if you know more, not 100% sure on what I said here.

ghry5 commented 5 months ago

@wiraki I was of the same opinion... However removing it did the trick for me. Possibly because i am using unixorn/fzf-zsh-plugin to automatically install fzf for me and that includes the eval? The only thing i can say is that it works great without it for me. That includes reverse history search with ctrl+r, file completion with ctrl+t and the **(tab) completion.