/fuzzyfinders.sh

Insert in Bash or ZSH a fuzzily found command-line, file or (recent) directory path at the cursor position.

Primary LanguageShell

The shell scripts fuzzyfinders.bash and fuzzyfinders.zsh set up key bindings in Bash and ZSH to insert, at the cursor position, a fuzzily found

  • command line of the history (by default bound to Ctrl-S),
  • file path (by default bound to Ctrl-T), listing first the recently (in the last $DAYS_LAST_MODIFIED) modified files,
  • the path of a subdirectory in the current directory (by default bound to Alt-C), listing first those recently changed to; uses either z.sh or z.lua, falls back to the in-built cdr in ZSH.

The file paths are listed among those inside the current working directory; in ZSH, optionally inside those after the path before the cursor position.

The fuzzy finder can be set by the variable $FUZZYFINDER and defaults, in this order, to the first fuzzy finder found among fzf, peco and fzy.

The functions _compgen_path (for files) and _compgen_dir (for directories) collect all paths and use by default the first file searcher among fd, ripgrep and ag, before falling back to the mandatorily present command find (on a UNIX system, according to the POSIX standard).

Installation

  1. Save these scripts, say to ~/.config/sh, and mark them executable by

    mkdir ~/Downloads
    cd ~/Downloads
    git clone https://github.com/Konfekt/fuzzyfinders.sh
    mkdir --parents ~/.config/sh
    cp ~/Downloads/fuzzyfinders.sh/fuzzyfinders.{bash,zsh} ~/.config/sh
    chmod a+x ~/.config/sh/fuzzyfinders.{bash,zsh}
  2. Source them on shell startup by adding to the shell configuration file

    • which is ~/.profile for Bash, the line
    . "$HOME/.config/sh/fuzzyfinders.bash"
    • respectively ~/.zshrc for ZSH, the line
    . "$HOME/.config/sh/fuzzyfinders.zsh"
  3. To use the Ctrl-S binding, add [ -n "${TTY:-}" ] && stty -ixon <"$TTY" >"$TTY" to your ~/.bashrc respectively unsetopt FLOW_CONTROL to your .zshrc.

Customization

The fuzzy finder can be set by the variable $FUZZYFINDER and defaults, in this order, to whichever fuzzy finder among sk, fzf, peco and fzy is present. The number of days for a file to be considered recently modified can by set by the variable $DAYS_LAST_MODIFIED

The key bindings can be set by the first argument of the command bind (in Bash) respectively bindkey (in ZSH).

Related

See vim-fuzzyfinders for the corresponding Vim plug-in.