In this article, we will discuss a common issue that some Zsh users encounter when trying to use the fzf library. Specifically, we will cover a situation where the user expects nvim to open instead of having fzf trigger it. This issue can occur when using the first available file in the current directory or in a netrw directory.
Prerequisites
Before diving into the issue, it is essential to have a basic understanding of the following concepts:
- Zsh: A Unix shell
- fzf: A general-purpose fuzzy finder for command-line interfaces
- nvim: Neovim, a popular text editor
Context
When using fzf in Zsh, it is common to have it trigger nvim when selecting a file. However, in some cases, the user might expect nvim to open directly instead of having fzf trigger it. This issue can occur when using the first available file in the current directory or in a netrw directory.
For instance, suppose you have the following files in your current directory:
file1.txt
file2.txt
file3.txt
If you try to open file1.txt using fzf and expect nvim to open directly, you might encounter the issue. Instead, fzf will open the file using the default editor, which could be vim or another editor.
Diagnosis
To diagnose this issue, you can check your Zsh configuration files, specifically the .zshrc file. Look for the following lines:
# Open file with nvim using fzf
source <$(brew --prefix zsh)/share/zsh-completions/zsh-completions/_fzf.zsh
source <$(brew --prefix zsh)/share/zsh-completions/zsh-completions/_fzf-nvim.zsh
fzf_map() {
emacsclient -nc "$1" &
}
zstyle ':completion:*' menu select
zstyle ':completion:*' list-colors ${(s.:m:33:)m} ${(s.:c:31:)c} ${(s.:h:35:)h} ${(s.:s:33:)s} ${(s.:t:32:)t} ${(s.:d:31:)d} ${(s.:i:34:)i} ${(s.:e:31:)e} ${(s.:f:31:)f} ${(s.:b:34:)b} ${(s.:W:31:)W} ${(s.:D:31:)D} ${(s.:#:30:)#}
zstyle ':completion:*' format '%F{%{[[$fg[1]==31]}-]%F{%{[[$fg[1]==31]}-]%F{red}%{[[$fg[1]==31]}-]%F{bold}%F{magenta}:%F{green}:%F{yellow}:%F{blue}:%F{cyan}:%F{white}:%F{red}%{[[$fg[1]==31]}-]%f%#} %B%d'
zstyle ':completion:*' label-prefix '( '
zstyle ':completion:*' label-suffix ')'
zstyle ':completion:*' verbose yes
zstyle ':completion:*' matcher-list 'miden'
zstyle ':completion:*' auto-menu enable
zstyle ':completion:*' word-list-file "/usr/share/zsh-5.3/plugins/zsh-autosuggestions/src/words.zsh"
zstyle ':completion:*' chars-used 'yes'
zstyle ':completion:*' insert-number 'enable'
zstyle ':completion:*' list-styles 'plain'
zstyle ':completion:*' prompt-description 'yes'
zstyle ':completion:*' completion-ignore-case 'yes'
zstyle ':completion:*' menu-select-trigger 'down'
zstyle ':completion:*' select-prompt-format ' %F{green}%m'
zstyle ':completion:*' auto-append-history enable
zstyle ':completion:*' auto-append-history-file ~/.zsh_history
zstyle ':completion:*' history-file ~/.zsh_history
zstyle ':completion:*' mark-directories off
zstyle ':completion:*' mark-symlinked-directories off
zstyle ':completion:*' add-zsh-hooks yes
zstyle ':completion:*' zsh-autosuggest-hook $fzf_bind_key
If you don't see the following lines:
source <$(brew --prefix zsh)/share/zsh-completions/zsh-completions/_fzf-nvim.zsh
fzf_map() {
emacsclient -nc "$1" &
}
You might need to add them to your .zshrc file. The first line sources the fzf-nvim completion, and the second line maps the fzf keybinding to open files with nvim.
Solution
To solve the issue, follow these steps:
- Open your .zshrc file in your favorite text editor:
- Add the following lines at the end of the file:
- Save and close the file.
- Restart your terminal or run:
nano ~/.zshrc
source <$(brew --prefix zsh)/share/zsh-completions/zsh-completions/_fzf-nvim.zsh
fzf_map() {
emacsclient -nc "$1" &
}
source ~/.zshrc
After following these steps, fzf should trigger nvim instead of opening the file with the default editor when you use the fzf_map keybinding.
References
For further reading, check out the following resources: