3

I would like to be able to type a filename in zsh, hit tab, and see a list of files that match that name in any subdirectories of my current directory. Then I could tab through those results and hit enter to select a certain file, just like the built-in zsh tab completion functionality works.

Example desired functionality:

$ emacs app.css [tab]
*www/css/app.css*    tmp/static/app.css
[enter]
$ emacs www/css/app.css

If this is not currently a feature, I assume I could write a custom completion script to implement it.

that other guy
  • 116,971
  • 11
  • 170
  • 194
Ben
  • 315
  • 1
  • 3
  • 14

1 Answers1

3

You can achieve something similar by utilizing the ** pattern (match over multiple directories) and by adding the control function _expand to the completer style (if not already the case).

As a minimum, you need to make the following settings:

autoload -U compinit
zstyle ':completion:*' completer _expand
compinit

Then you can get the desired list with

% emacs **/app.css[TAB]

It even allows for the option to open all matching files at once.

Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • I really like this solution but when I put it in my .zshrc file it prevents the standard tab complete from working. Where do I need to put this so I can make it work with standard tab complete? I'm using zsh-autosuggestions, I am a software dev but I've not done shell scripting before. – Harry Len Feb 20 '20 at 11:10
  • From what I can see, it should not interfere with zsh-autosuggestions. But if `compinit` is already configured and loaded in your `~/.zshrc`, then just adding this as it is might disable some settings. Check, if you already have any of these lines in your configuration, in which case you can just add `_expand` to the list of arguments after `completer`. For example, I have `zstyle ':completion:*' completer _expand _complete _approximate` in my own configuration. If you do not have these lines (especially `compinit`), you might not be using the "new" (since 2000) completion system. – Adaephon Feb 26 '20 at 10:24