diff --git a/plugins/fzf/README.md b/plugins/fzf/README.md index 69e22bb15..c5283a5be 100644 --- a/plugins/fzf/README.md +++ b/plugins/fzf/README.md @@ -1,11 +1,50 @@ # fzf plugin -The fzf plugin enables fzf keybindings and completions on bash. +The fzf plugin enables [fzf](https://github.com/junegunn/fzf) key bindings and fuzzy completion in Bash. -To use it, install -[fzf](https://github.com/junegunn/fzf?tab=readme-ov-file#installation) and add fzf -to the plugins array of your bashrc file: +## Installation + +### Install fzf + +**Package manager / mise / asdf:** ```bash -plugins=(... fzf) +# Homebrew +brew install fzf + +# mise +mise use -g fzf ``` + +**Git (official method):** + +```bash +git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf +~/.fzf/install --no-update-rc # skip the ~/.bashrc modification, the plugin handles it +``` + +> **Note:** If you installed fzf via git, pass `--no-update-rc` to the installer (or answer *No* when asked to update `.bashrc`). The plugin automatically adds `~/.fzf/bin` to your `$PATH` — no need for `source ~/.fzf.bash`. + +### Enable the plugin + +```sh +plugins=(fzf) +``` + +## Supported installation locations + +The plugin searches for fzf in the following locations, in order: + +1. Already in `$PATH` (package managers, mise, asdf, manual installs) +2. `~/.fzf/bin` (git clone install) +3. `${XDG_CONFIG_HOME:-$HOME/.config}/fzf/bin` + +## Key bindings + +Once active, fzf provides these shell key bindings: + +| Binding | Description | +|-------------|--------------------------------------------| +| `Ctrl+T` | Paste selected file paths into the command | +| `Ctrl+R` | Search command history | +| `Alt+C` | `cd` into the selected directory | diff --git a/plugins/fzf/fzf.plugin.sh b/plugins/fzf/fzf.plugin.sh index 4c1ff50a4..4e631ba95 100644 --- a/plugins/fzf/fzf.plugin.sh +++ b/plugins/fzf/fzf.plugin.sh @@ -1,9 +1,40 @@ #! bash oh-my-bash.module +# Description: Integrate fzf (https://github.com/junegunn/fzf) key bindings and fuzzy completion -# Check if fzf is installed -if _omb_util_command_exists fzf; then - # Set up fzf key bindings and fuzzy completion - eval -- "$(fzf --bash)" -else +# -- Locate fzf --------------------------------------------------------------- +# Support common installation methods: +# 1. Already in $PATH (package manager, mise/asdf, manual) +# 2. Git install: ~/.fzf/bin +# 3. XDG config dir: ~/.config/fzf/bin (less common) +if ! _omb_util_command_exists fzf; then + if [[ -x $HOME/.fzf/bin/fzf ]]; then + PATH="${PATH:+$PATH:}$HOME/.fzf/bin" + elif [[ -x ${XDG_CONFIG_HOME:-$HOME/.config}/fzf/bin/fzf ]]; then + PATH="${PATH:+$PATH:}${XDG_CONFIG_HOME:-$HOME/.config}/fzf/bin" + fi +fi + +if ! _omb_util_command_exists fzf; then _omb_util_print '[oh-my-bash] fzf not found, please install it from https://github.com/junegunn/fzf' >&2 + return +fi + +# -- Activate ----------------------------------------------------------------- +# fzf >= 0.46 ships `fzf --bash` which emits key bindings + completions in one go. +# Older git installs ship separate shell scripts under ~/.fzf/shell/. +if fzf --bash &>/dev/null; then + eval "$(fzf --bash)" +else + # Fallback for fzf < 0.46 git installs + local _omb_plugin_fzf_base + _omb_plugin_fzf_base=$(command -v fzf 2>/dev/null) + # Ensure we got a real filesystem path, not a function/alias name + if [[ -x $_omb_plugin_fzf_base ]]; then + _omb_plugin_fzf_base=${_omb_plugin_fzf_base%/bin/fzf} + [[ -s $_omb_plugin_fzf_base/shell/key-bindings.bash ]] && + source "$_omb_plugin_fzf_base/shell/key-bindings.bash" + [[ $- == *i* && -s $_omb_plugin_fzf_base/shell/completion.bash ]] && + source "$_omb_plugin_fzf_base/shell/completion.bash" + fi + unset -v _omb_plugin_fzf_base fi