6

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.

  • 1
    ag mymethod(param) is a syntax error in all the other shells. Disabling one of the most powerful features of zsh just for that sounds a bit silly to me. Just quote that. (ag 'mymethod(param)' like in all the other shells). – Stéphane Chazelas Aug 7 '14 at 22:31
  • :( yeah I'm starting to be convinced that this is a futile effort... – Josh Aug 7 '14 at 22:38
  • 3
    You can do alias ag='noglob ag' if you want to disable globbing in the arguments to ag. – Stéphane Chazelas Aug 7 '14 at 22:47
  • I second Stéphane. What's ag? – Gilles 'SO- stop being evil' Aug 8 '14 at 0:18
4

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'
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.

  • Is the right command setopt sh_glob? (zsh can't find a shopt command) I tried setopt but it still complains zsh: missing end of name. – Josh Aug 7 '14 at 22:37
  • @Josh Yes, the name is setopt. – Gilles 'SO- stop being evil' Aug 8 '14 at 0:17
  • @Gilles Thanks for the correction; muscle memory is hard to overcome :) – chepner Aug 8 '14 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.