fish fzf completions
fifc brings fzf powers on top of fish completion engine and allows customizable completion rules
- fish 3.4.0+
- Preview/open any file: text, image, gif, pdf, archive, binary (using external tools)
- Preview/open command's man page
- Preview/open function definitions
- Preview/open full option description when completing commands
- Recursively search for files and folders when completing paths (using fd)
- Preview directory content
- Preview process trees (using procs)
- Modular: easily add your own completion rules
- Properly handle paths with spaces (needs fish 3.4+)
fisher install gazorby/fifc
You only need to set one setting after install:
set -Ux fifc_editor <your-favorite-editor>
And enjoy built-in completions!
By default fifc override tab
, but you can assign another keybinding:
# Bind fzf completions to ctrl-x
set -U fifc_keybinding \cx
fifc can use modern tools if available:
Prefer | Fallback to | Used for | Custom options |
---|---|---|---|
bat | cat | Preview files | $fifc_bat_opts |
chafa | file | Preview images, gif, pdf etc | $fifc_chafa_opts |
hexyl | file | Preview binaries | $fifc_hexyl_opts |
fd | find | Complete paths | $fifc_fd_opts |
exa | ls | Preview directories | $fifc_exa_opts |
ripgrep | pcregrep | Search options in man pages | - |
procs | ps | Complete processes and preview their tree | $fifc_procs_opts |
broot | - | Explore directory trees | $fifc_broot_opts |
Custom options can be added for any of the commands used by fifc using the variable mentioned in the above table.
Example:
Show line number when previewing files:
set -U fifc_bat_opts --style=numbers
Show hidden file by default:
set -U fifc_fd_opts --hidden
set -U fifc_exa_opts --icons --tree
Custom rules can easily be added using the fifc
command. Actually, all builtin rules are added this way: see conf.d/fifc.fish
See fifc -h
for more details.
Basically, a rule allows you to trigger some commands based on specific conditions.
A condition can be either:
- A regex that must match commandline before the cursor position
- An arbitrary command that must exit with a non-zero status
If conditions are met, you can bind custom commands:
- preview: Command used for fzf preview
- source: Command that feeds fzf input
- open: Command binded to
fifc_open_keybinding
(defaults to ctrl-o)
All commands have access to the following variable describing the completion context:
Variable | Description | Command availability |
---|---|---|
fifc_candidate |
Currently selected item in fzf | all except source |
fifc_commandline |
Commandline part before the cursor position | all |
fifc_token |
Last token from the commandline | all |
fifc_group |
Group to which fish suggestions belong (possible values: directories, files, options or processes) | all |
fifc_extracted |
Extracted string from the currently selected item using the extracted regex, if any |
all except source |
fifc_query |
fzf query. On source command, it is the initial fzf query (passed through --query option) |
all |
fifc test completion items to set fifc_group
with the following conditions:
Group | Condition |
---|---|
directories | All completion items are directories |
files | Items can be either files or directories |
options | All items match the following regex: \h+\-+\h*$ |
processes | All items match the following regex ^[0-9]+$ (list of PID) |
By default, fifc evaluate all rules in the order in which they have been defined and stops at the first where all conditions are met. It does this each time it has to resolve source, preview and open commands.
Take the following scenario:
# Rule 1
fifc -n 'test "$fifc_group" = files' -p 'bat $fifc_candidate'
# Rule 2
fifc -n 'string match "*.json" "$fifc_candidate"' -p 'bat -l json $fifc_candidate'
When completing path, $fifc_group
will be set to "files" so the first rule will always be valid in that case, and the second one will never be reached.
Another example:
# Rule 1
fifc --condition 'test "$fifc_group" = files' --preview 'bat $fifc_candidate'
# Rule 2
fifc --condition 'test "$fifc_group" = files' --source 'fd . --color=always --hidden $HOME'
Here, even if both rules have the same conditions, they won't interfere because fifc has to resolve source commands before the preview commands, so order doesn't matter in this case.
If you want to write your own rule based on the same conditions as one of the built-in ones, you can use fifc --order
option.
It tells fifc to evaluate the rule in a predefined order, so you can set it to 1 to make sure it will be evaluated first.
When omitting the --order
, the rule will be declared unordered and will be evaluated after all other ordered rules, and all other unordered rules defined before.
All built-in rules are unordered.
Here is how the built-in rule for file preview/open is implemented:
fifc \
# If selected item is a file
-n 'test -f "$fifc_candidate"' \
# bind `_fifc_preview_file` to preview command
-p _fifc_preview_file \
# and `_fifc_preview_file` when pressing ctrl-o
-o _fifc_open_file
Interactively search packages in archlinux:
fifc \
-r '^(pacman|paru)(\\h*\\-S)?\\h+' \
-s 'pacman --color=always -Ss "$fifc_token" | string match -r \'^[^\\h+].*\'' \
-e '.*/(.*?)\\h.*' \
-f "--query ''" \
-p 'pacman -Si "$fifc_extracted"'
Search patterns in files and preview matches when commandline starts with **<pattern>
(using ripgrep and batgrep):
fifc \
-r '.*\*{2}.*' \
-s 'rg --hidden -l --no-messages (string match -r -g \'.*\*{2}(.*)\' "$fifc_commandline")' \
-p 'batgrep --color --paging=never (string match -r -g \'.*\*{2}(.*)\' "$fifc_commandline") "$fifc_candidate"' \
-f "--query ''" \
-o 'batgrep --color (string match -r -g \'.*\*{2}(.*)\' "$fifc_commandline") "$fifc_candidate" | less -R' \
-O 1
Thanks PatrickF1 (and collaborators!), for the great fzf.fish plugin which inspired me for the command-based configuration, and from which I copied the ci workflow.