The problem:
Sometimes, when I try to do things in zsh, it won't complete the filenames I want because they don't apply to the current context. Two examples:
git diff
to diff two arbitrary files on my hard drive. These files do not reside in any repository, but who cares?! git diff --no-index
is a really nice way to diff any two files. But because they aren't in a repo, zsh won't complete them.The proposed solution:
I could simply edit the source control context to complete all filenames, regardless of their source control status. But there are a couple limitations:
So, I decided instead to bind a key shortcut to force normal, context-free file completion.
What I have so far:
Zsh apparently has an easy way of doing this, as detailed in this question. So I've added the following lines to my .zshrc
:
zle -C complete complete-word complete-files
bindkey '^[[Z' complete
complete-files () { compadd - $PREFIX* }
This causes shift-tab to initiate file completion. It works beautifully in standard zsh! But boo-hoo, for some reason it doesn't work when I source oh-my-zsh. :-(
So is there a way to get this to work with oh-my-zsh, or is there an alternative solution I might find satisfying?
My way of doing exactly that (replacing the default diff
command with the one from git
) was pretty straightforward.
Add this to your ~/.zshrc
:
diff() {
git diff --no-index "$@"
}
Before I was using an alias, but since zsh can "solve" them, it was then using the autocompletion functions from git diff
, which inadvertently led to the same problem as you.
Using a function solves this issue as zsh reverts to the classic "files only" completion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With