Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force filename completion under oh-my-zsh?

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:

  1. I want to force-add a file which is currently ignored in hg or git. oh-my-zsh tries to help me by only completing non-ignored files, but in this case it is a hindrance. I'm forced to type the full path to the file manually.
  2. I want to use 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:

  1. I don't know how to do that.
  2. It might be nice to have an "escape hatch" whereby I could force file completion, no matter what the context.

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?

like image 304
Neil Traft Avatar asked Nov 10 '22 03:11

Neil Traft


1 Answers

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.

like image 76
Antoine Bolvy Avatar answered Dec 20 '22 05:12

Antoine Bolvy