Search code examples
zshzsh-completion

bind key to complete filename wherever the context is in Zsh


Sometimes I want a filename instead of what zsh guesses for me. For example, I have a PNG file without a proper .png suffix, which makes zsh think it isn't a picture and won't list it when I type Tab to complete arguments of display.

I am wondering if there is a key sequence that completes for a filename wherever the context is, like ^XC for _correct_filename, or how to configure zsh to do the thing?


Solution

  • You can add a key binding to do what you want:

    zle -C complete complete-word complete-files
    bindkey '^X\t' complete
    complete-files () { compadd - $PREFIX* }
    

    Edit: Added $PREFIX

    You can add those lines to your ~/.zshrc file.

    That makes the completion list all files when you press Ctrl-x Tab at each step instead of Tab. You could choose another keystroke combination that suits you.

    Or to make ImageMagick completions always include all files, try editing (make a backup first) the file /usr/share/zsh/functions/Completion/Unix/_imagemagick (or similar) and change this to comment out the existing line that begins with _files and add the new one shown:

    if (( $# )); then
      # _files "$@" -g "*.(#i)(${~formats//:/|})(-.)"
      _files "$@"
      return
    fi