TransWikia.com

zsh file tab completion: path preceded by non-space character

Unix & Linux Asked by sameers on October 31, 2021

I couldn’t figure out a way to search for this and see if it is already explained on the Internet.

I have the following at the command line and I want to hit <TAB> at the end to start file completion:

   mycmd process file=data/old/1/

When I was using Bash, that TAB would show me the files in the folder 1/ but with Zsh that doesn’t happen unless I enter a space after the = sign. Can I reconfigure Zsh to do that?

One Answer

setopt magic_equal_subst

This works both with old-style and new-style completion, i.e. with or without compinit.

Completion of file names after = is an obscure and undocumented side effect of the magic_equal_subst option. The documented effect of this option is to enable filename expansion after =.

Most of the time, if you notice the effect of this option at all, it's a desirable effect, e.g.

somecommand --option=~/file

passes a path under your home directory to somecommand, not a path in a directory called ~. However, this also has the usually undesirable effect that an argument == triggers filename expansion:

somecommand foo==bar

passes foo=/bin/bar if the command bar is found in $PATH at /bin/bar. If you want to pass == literally to the command, at least one of the equal signs needs to be quoted, e.g.

somecommand foo==bar
somecommand foo==bar

In the new-style completion system, this is handled inside _default. If you want to get filename completion after = without enabling magic_equal_subst, you need to override or wrap around _default to obtain the same effect. For example:

autoload +X -U _default
functions[_default]="setopt local_options magic_equal_subst; $functions[_default]"

Answered by Gilles 'SO- stop being evil' on October 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP