8

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
lilydjwg
  • 1,621
  • 20
  • 41

2 Answers2

9

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
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Useful but not very ideal. The `complete-files` function can only complete files under current directory. I've tried `_files` instead of it but got a `(eval):1: no matches found: *:all-files` error. Is there a way to make it behave like when completing after `cat`? I want it to complete not only for `display`, so the latter doesn't suit my situation. – lilydjwg Apr 18 '10 at 07:21
  • @lilydjwg: I don't understand. For me, complete-files functions with completing for display the same way that regular completion works for cat (except the file-type suffixes such as "@" and "*" aren't added. When I do regular tab-completion for cat I only get files in the current directory. – Dennis Williamson Apr 18 '10 at 10:44
  • I mean, if I type a path (eg ./ or ../ or abc/, with '/' in what I've typed) and then trigger the function, nothing will be shown. – lilydjwg Apr 19 '10 at 13:58
  • @lilydjwg: I added the `$PREFIX` variable in the `complete` function. See if that helps. – Dennis Williamson Apr 19 '10 at 14:57
5

Dennis' answer didn't expand tilde for me, so I would get stuff like complete-files: no matches found: ~/ma* when I tried to invoke it on foo ~/ma. I did find an alternate in the zsh FAQ that will expand them, though:

zle -C complete-file complete-word _generic
zstyle ':completion:complete-file::::' completer _files
bindkey '^xF' complete-file
sfaleron
  • 61
  • 1
  • 3