Skip to content

zsh: make Ctrl-T file widget “path-prefix aware” (same ^T, no new keybind) #4656

@prateek-oai

Description

@prateek-oai

Checklist

  • I have read through the manual page (man fzf)
  • I have searched through the existing issues
  • For bug reports, I have checked if the bug is reproducible in the latest version of fzf

Output of fzf --version

0.66.1

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Shell

  • bash
  • zsh
  • fish

Problem / Steps to reproduce

In zsh, the default Ctrl-T widget (fzf-file-widget from shell/key-bindings.zsh) always searches relative to $PWD and appends the selection(s) to the end of $LBUFFER.

This makes Ctrl‑T awkward when the cursor is already inside a path prefix (common workflow):

  1. Type: cat /dirB/pa
  2. Press Ctrl‑T
  3. Ctrl‑T still searches from $PWD, and appends results to the end (instead of completing/replacing pa under /dirB/).

Related discussion: #2791

In that thread, the suggested solution is to use fuzzy completion / dedicated completion key (e.g. /dirB/**<TAB>), which works but uses a different keybinding and a different mechanism than Ctrl‑T. I’d like Ctrl‑T
itself to handle the “path prefix is present” case.

Proposed behavior (zsh Ctrl‑T)

When Ctrl‑T is invoked and the current token (under cursor) looks like a path prefix:

  • Treat the directory part as the search root
  • Seed fzf’s initial query with the basename fragment already typed
  • Replace the token under cursor with the selected path(s) (rather than appending to the end of LBUFFER)

Examples:

  • cat /dirB/<C-t> should search under /dirB
  • cat /dirB/pa<C-t> should search under /dirB with initial query pa, and replace pa with the selected entry/entries

When there is no path prefix, keep current behavior unchanged.

Implementation sketch

fish already does something similar by parsing the current token into (dir, query, prefix) and invoking fzf with --walker-root=$dir --query=$query (see shell/key-bindings.fish).

For zsh, a minimal/opt-in approach could be:

  • detect the current token (ideally under cursor; at minimum, last whitespace-delimited token)
  • if it contains a valid dir prefix, pass --walker-root + --query accordingly
  • replace the token under cursor with selections

If changing default Ctrl‑T behavior is undesirable, this could be guarded behind an opt-in env var (default off) that switches fzf-file-widget into this “path-prefix aware” mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions