fzf icon indicating copy to clipboard operation
fzf copied to clipboard

Feature request: ctrl-t start with current word in search

Open kaddkaka opened this issue 10 months ago • 5 comments

Checklist

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

Output of fzf --version

0.58.0 (65db735)

OS

  • [x] Linux
  • [ ] macOS
  • [ ] Windows
  • [ ] Etc.

Shell

  • [x] bash
  • [ ] zsh
  • [ ] fish

Problem / Steps to reproduce

This is a new request for an existing closed issue: #3195 "Option for Ctrl-T to use current command-line content as a search pattern"

I believe I have a usecase that wasn't discussed in the previous issue where invoking ctrl-T and using the current WORD as a starting point would be useful: Opening png files with firefox.

The issue is that firefox tries to be helpful and among the completions doesn't include files it thinks you don't want to open. Example:

mkdir pictures
touch pictures/potato.png
firefox pictures/<TAB>

Result: potato.png does not get completed.

My FZF configuration:

source "$XDG_CONFIG_HOME/fzf/key-bindings.bash"
source "$XDG_CONFIG_HOME/bash/fzf-bash-completion.sh"
bind -x '"\t": fzf_bash_completion'

Firefox does support opening png:s so this is arguably an issue with firefox, but it would be nice to circumvent it by invoking ctrl-t as described here.

Questions: Are there situations when one would prefer that [a-z/] followed by <TAB> doesn't include the current word in the search pattern?

kaddkaka avatar Feb 23 '25 09:02 kaddkaka

Have you tried using fuzzy completion? i.e. firefox pictures/**<TAB>

My FZF configuration:

source "$XDG_CONFIG_HOME/fzf/key-bindings.bash"
source "$XDG_CONFIG_HOME/bash/fzf-bash-completion.sh"
bind -x '"\t": fzf_bash_completion'

This is quite different from our current recommendation.

  • https://github.com/junegunn/fzf?tab=readme-ov-file#setting-up-shell-integration

Could you share how you ended up with it?

junegunn avatar Feb 24 '25 13:02 junegunn

I probably invented source "$XDG_CONFIG_HOME/fzf/key-bindings.bash by myself, can't remember. The other two lines of config are from: https://github.com/lincheney/fzf-tab-completion

I removed that and tried with eval "$(fzf --bash)" as per the instructions now, and ** on it's own works, but firefox ** does not show anything. (Is firefox hijacking the completion mechanism before fzf get's to do anything?)

In a folder with this content:

.
├── a.html
├── b.png
├── c.svg
├── d.xml
└── subdir
  1. ls<TAB> lists all 5
  2. ls **<TAB> also lists all 5
  3. firefox <TAB> lists a.html and subdir
  4. firefox **<TAB> lists none

kaddkaka avatar Feb 24 '25 14:02 kaddkaka

Does complete | grep firefox show anything?

junegunn avatar Mar 03 '25 13:03 junegunn

Yes, it shows a function:

$ complete | fzf
complete -F _firefox firefox
$ declare -f _firefox
_firefox ()
{
    local cur prev words cword split;
    _init_completion -s || return;
    [[ $cur == -MOZ_LOG*=* ]] && prev=${cur%%=*} cur=${cur#*=};
    case $prev in
        --help | --version | --display | --UILocale | -MOZ_LOG | --new-window | --new-tab | --private-window | --window-size | --search | --start-debug
            return
        ;;
        --profile | --screenshot)
            _filedir -d;
            return
        ;;
        -MOZ_LOG_FILE)
            _filedir log;
            return
        ;;
        --recording-file)
            _filedir;
            return
        ;;
        --debugger | -d)
            COMPREPLY=($(compgen -c -- "$cur"));
            return
        ;;
    esac;
    $split && return;
    if [[ $cur == -* ]]; then
        COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"));
        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace;
        return;
    fi;
    _filedir "@(?([xs])htm?(l)|pdf|txt)"
}

kaddkaka avatar Mar 07 '25 11:03 kaddkaka

That explains. In bash, fuzzy completion only works for a predefined set of commands and commands with no completion defined. Because firefox has its own completion in your environment, fuzzy completion is not triggered. To enable fuzzy completion for firefox, follow the instruction in https://github.com/junegunn/fzf?tab=readme-ov-file#supported-commands.

junegunn avatar Mar 07 '25 11:03 junegunn