Home

Awesome

FZF integration for Clink

This script integrates the FZF "fuzzy finder" with the Clink command line editing enhancements for CMD.exe on Windows.

[!TIP] Consider using clink-gizmos instead, which includes this script as well as many other useful scripts. If you use clink-gizmos, then you don't need clink-fzf -- clink-gizmos contains a collection of scripts, and clink-fzf contains a single script.

How to install

  1. Copy the fzf.lua file into your Clink scripts directory (you can run clink info to find it, or see Location of Lua Scripts in the Clink docs for more info).

  2. Either put fzf.exe in a directory listed in the system PATH environment variable, or run clink set fzf.exe_location <put_full_exe_name_here> to tell Clink where to find the FZF program.

  3. Set up key bindings.

    • To use the default key bindings, run clink set fzf.default_bindings true.
    • To use custom key bindings, add them to your .inputrc file (see Key Bindings).

How to use

For detailed information on using FZF, please refer to the FZF documentation.

Here are the default key bindings in Clink, if you've enabled the fzf.default_bindings setting:

KeyDescription
<kbd>Ctrl</kbd>+<kbd>T</kbd>Lists files recursively; choose one or multiple to insert them.
<kbd>Ctrl</kbd>+<kbd>R</kbd>Lists history entries; choose one to insert it<br>Press <kbd>Del</kbd> in the list to delete the selected history entry.<br>Press <kbd>Ctrl</kbd>+<kbd>R</kbd> in the history list to toggle fzf's sorting mode.
<kbd>Alt</kbd>+<kbd>C</kbd>Lists subdirectories; choose one to 'cd /d' to it.
<kbd>Alt</kbd>+<kbd>B</kbd>Lists key bindings; choose one to invoke it.
<kbd>Tab</kbd>Uses fzf to filter match completions, but only when preceded by '**' (recursive).
<kbd>Ctrl</kbd>+<kbd>Space</kbd>Uses fzf to filter match completions (and supports '**' for recursive).

Note: For the default key bindings to work, you must be using Clink v1.2.46 or higher. If you're using an older version of Clink then consider upgrading Clink, or manually add key bindings in your .inputrc file as described below.

Recursive completion

You can use **<kbd>Tab</kbd> to list files recursively under the directory.

For example:

Overriding the default behaviors

Key bindings

You can use your own custom key bindings if you prefer.

Run clink set fzf.default_bindings false and add key bindings to your .inputrc file manually.

Run clink echo to find key bindings strings. See Key Bindings in the Clink documentation for more information on how to set key bindings.

The default key bindings for FZF are listed here in .inputrc file format for convenience:

# Default key bindings for fzf with Clink.
"\C-t":        "luafunc:fzf_file"           # Ctrl+T lists files recursively; choose one or multiple to insert them.
"\C-r":        "luafunc:fzf_history"        # Ctrl+R lists history entries; choose one to insert it.
"\M-c":        "luafunc:fzf_directory"      # Alt+C lists subdirectories; choose one to 'cd /d' to it.
"\M-b":        "luafunc:fzf_bindings"       # Alt+B lists key bindings; choose one to invoke it.
"\t":          "luafunc:fzf_complete"       # Tab uses fzf to filter match completions, but only when preceded by '**' (recursive).
"\e[27;5;32~": "luafunc:fzf_complete_force" # Ctrl+Space uses fzf to filter match completions (and supports '**' for recursive).

The following commands are also available for binding to keys:

CommandDescription
"luafunc:fzf_menucomplete"Use fzf for completion after ** otherwise use the menu-complete command.
"luafunc:fzf_oldmenucomplete"Use fzf for completion after ** otherwise use the old-menu-complete command.

FZF options

You can specify FZF options for each of the different commands:

Env Var NameDescription
FZF_DEFAULT_OPTSOptions that are applied to all fzf invocations.
FZF_CTRL_T_OPTSOptions for <kbd>Ctrl</kbd>+<kbd>T</kbd> (the "luafunc:fzf_file" function).
FZF_CTRL_R_OPTSOptions for <kbd>Ctrl</kbd>+<kbd>R</kbd> (the "luafunc:fzf_history" function).
FZF_ALT_C_OPTSOptions for <kbd>Alt</kbd>+<kbd>C</kbd> (the "luafunc:fzf_directory" function).
FZF_BINDINGS_OPTSOptions for <kbd>Alt</kbd>+<kbd>B</kbd> (the "luafunc:fzf_bindings" function).
FZF_COMPLETION_OPTSOptions for the completion functions ("luafunc:fzf_complete", "luafunc:fzf_menucomplete", "luafunc:fzf_oldmenucomplete", "luafunc:fzf_selectcomplete", etc).

[!TIP] The options in FZF_DEFAULT_OPTS are added to all fzf commands. The options in the other ..._OPTS environment variables are added only to their corresponding fzf commands.

File and directory list commands

You can specify commands to run for collecting files or directories:

Env Var NameDescription
FZF_CTRL_T_COMMANDCommand to run for collecting files for <kbd>Ctrl</kbd>+<kbd>T</kbd> (the "luafunc:fzf_file" function).
FZF_ALT_C_COMMANDCommand to run for collecting directories for <kbd>Alt</kbd>+<kbd>C</kbd> (the "luafunc:fzf_directory" function).

Icons in FZF

You can optionally have file icons show up in FZF completion lists in Clink.

Requirements:

Configure the following environment variables:

If you want it to recurse into hidden directories, then remove the /X:d part from the commands in the environment variables.

If you want it to list hidden files and directories, then remove the -h part at the end of the /a: flags in the environment variables.

Previewing file and folder contents

You can specify a preview command for FZF.

If you have enabled icons, then replace each {} with {2..}.

The examples also let you toggle between different preview window sizes with <kbd>Ctrl</kbd>+<kbd>/</kbd>.

Previewing file contents

The command will show the contents of files with the <kbd>Ctrl</kbd>+<kbd>T</kbd> hotkey; it assumes you have bat installed and available in the %PATH% environment variable.

set FZF_CTRL_T_OPTS=--preview-window "right:40%,border-left" --bind "ctrl-/:change-preview-window(right:70%|hidden|)" --preview "bat --force-colorization --style=numbers,changes --line-range=:500 -- {}"

Previewing folder contents

This command will show the contents of folders with the <kbd>Alt</kbd>+<kbd>C</kbd> hotkey; it assumes you have dirx installed and available in the %PATH% environment variable.

set FZF_ALT_C_OPTS=--preview-window "right:40%,border-left" --bind "ctrl-/:change-preview-window(right:70%|hidden|)" --preview "dirx -b -s --bare-relative --utf8 --level=3 --tree --icons=always -- {}"

Previewing image files

The sample script fzf-preview.cmd can be used to preview image files. It assumes you have both chafa and bat installed and available in the %PATH% environment variable.

To use the sample script for previewing image files, replace the --preview "..." part of the examples above with either:

If your terminal supports sixels, you can also run set CLINK_FZF_PREVIEW_SIXELS=1 to tell the fzf-preview.cmd script to tell chafa to use sixels.

If you want to customize the flags for chafa and bat:

  1. Make a copy of the script.
  2. Customize the copy instead of the original. That way, your changes won't get overwritten when you update clink-fzf (or clink-gizmos).
  3. Replace fzf-preview.cmd in the examples above with the filename of your customized copy.

Unicode content in FZF

FZF assumes UTF8 stdin, but native Windows programs assume stdin and stdout are in the console's current codepage, which is not UTF8 by default.

The fzf.lua script automatically saves the current codepage and switches to UTF8 before invoking FZF, and then restores the original codepage after FZF exits. This should ensure that users don't need to apply any custom workarounds to get Unicode text to show up in FZF.

For curious readers, here are related issues in FZF: