tags:

views:

126

answers:

1

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?

+3  A: 

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
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
@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
@Dennis Williamson: 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
@lilydjwg: I added the `$PREFIX` variable in the `complete` function. See if that helps.
Dennis Williamson
@Dennis Williamson: It works nicely! Thank you!
lilydjwg