Disable glob qualifiers in zsh
Solution 1
You can disable the PATTERN(QUALIFIERS)
syntax by unsetting the bare_glob_qual
option:
setopt no_bare_glob_qual
If the option extended_glob
is set (and you should set it, the only reason not to set it is for backward compatibility with rare scripts that use unusual syntax), then there is another syntax for glob qualifiers: PATTERN(#qQUALIFIERS)
. So you can still use glob qualifiers, which are one of zsh's killer features, but you'll have to type a bit more.
Zsh lets you disable wildcard expansion (globbing) altogether, and this looks like a better choice for you. If a command is prefixed by noglob
, then no globbing is performed on its arguments. For example, to be able to type URLs containing ?
as arguments to wget
, I have alias wget='noglob wget'
. If you set alias ag='noglob ag'
, you can type ag mymethod(param)
.
If ag
takes both a search pattern and file names as arguments, then disabling globbing is not good. If you're able to parse the arguments of ag
, then you can perform wildcard expansion on them. I don't know the syntax of ag
, so I'll give an example where I assume that ag
only takes options that don't take arguments, and that its first non-option argument is a pattern and the rest are files.
ag () {
local i=1
while [[ ${(P)i} = -* ]]; do ((++i)); done
if ((i < $#)); then
set -- "${(@)@[1,$i]}" $~@[$((i+1)),$#]
fi
}
alias ag='noglob ag'
Solution 2
Set the SH_GLOB
option.
setopt sh_glob
From man zshoptions
:
SH_GLOB Disables the special meaning of `(', `|', `)' and `<' for globbing the result of parameter and command substitutions, and in some other places where the shell accepts patterns. If SH_GLOB is set but KSH_GLOB is not, the shell allows the interpretation of subshell expressions enclosed in parentheses in some cases where there is no space before the opening parenthesis, e.g. !(true) is interpreted as if there were a space after the !. This option is set by default if zsh is invoked as sh or ksh.
Related videos on Youtube
Coralie BOUCHERON
Updated on September 18, 2022Comments
-
Coralie BOUCHERON over 1 year
Is there a way to disable glob qualifiers with zsh? It seems like a useful feature, but it gets in the way of normal parentheses.
I use
ag
to search files, andag mymethod(param)
causes zsh to complain aboutmissing end of name
, and I'd rather not have to quote my arguments. Since I never really use glob qualifiers, I figured I'd just disable them, but my Googling hasn't turned up any results.-
Admin almost 10 years
ag mymethod(param)
is a syntax error in all the other shells. Disabling one of the most powerful features ofzsh
just for that sounds a bit silly to me. Just quote that. (ag 'mymethod(param)'
like in all the other shells). -
Admin almost 10 years:( yeah I'm starting to be convinced that this is a futile effort...
-
Admin almost 10 yearsYou can do
alias ag='noglob ag'
if you want to disable globbing in the arguments toag
.
-
-
Coralie BOUCHERON almost 10 yearsIs the right command
setopt sh_glob
? (zsh can't find ashopt
command) I tried setopt but it still complains zsh: missing end of name. -
Gilles 'SO- stop being evil' almost 10 years@Josh Yes, the name is
setopt
. -
chepner almost 10 years@Gilles Thanks for the correction; muscle memory is hard to overcome :)
-
Juan José Ramírez about 3 yearscan this option be added
~/.zshrc
somehow?