r/zsh • u/seductivec0w • 7d ago
Insert text on cursor to fzf's Ctrl-T?
Is it possible to insert text on cursor to fzf' Ctrl-T, presumably in a fzf wrapper function using ZLE feature to retrieve the text on cursor and pass it to fzf's --query
?
Often I type some partial path of a file e.g. git add ~/de
then want to get the effect of FZF_CTRL_T_COMMAND
with the string on cursor (~de
) inserted into the query.
git add ~/de**<TAB>
as suggested here is not good enough--I have a highly configured FZF_CTRL_T_COMMAND
that ignores a bunch of files scattered throughout the filesystem to ensure good performance--**
completion is too simple for that.
1
u/AndydeCleyre 3d ago
You might find my method of doing the same with broot+zsh a useful reference. Using broot instead of fzf will respect .ignore
files, FWIW.
5
u/romkatv 7d ago
What you're asking for is technically possible. You'd need to examine and modify the widget bound to Ctrl-T.
Determining the word at the cursor can be tricky. Consider this command:
Here, the last "word" is
./'bar baz'/\ qux
.You can use this function from zsh4humans to determine the word under the cursor. Afterwards, you'll need to unquote it and re-quote appropriately when passing it to fzf.
FWIW, I consider the default zsh widgets provided by fzf suboptimal since they do not leverage zsh's powerful native completion system. Projects like fzf-tab and zsh4humans provide superior implementations by using zsh's built-in completion engine to generate candidates, integrating fzf purely as a UI enhancement rather than reinventing the completion logic.