Disable glob qualifiers in zsh

wildcardszsh

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, and ag mymethod(param) causes zsh to complain about missing 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.

Best Answer

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'