Disable glob qualifiers in zsh

13,132

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.

Share:
13,132

Related videos on Youtube

Coralie BOUCHERON
Author by

Coralie BOUCHERON

Updated on September 18, 2022

Comments

  • Coralie BOUCHERON
    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, 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.

    • Admin
      Admin almost 10 years
      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).
    • Admin
      Admin almost 10 years
      :( yeah I'm starting to be convinced that this is a futile effort...
    • Admin
      Admin almost 10 years
      You can do alias ag='noglob ag' if you want to disable globbing in the arguments to ag.
  • Coralie BOUCHERON
    Coralie BOUCHERON almost 10 years
    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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 10 years
    @Josh Yes, the name is setopt.
  • chepner
    chepner almost 10 years
    @Gilles Thanks for the correction; muscle memory is hard to overcome :)
  • Juan José Ramírez
    Juan José Ramírez about 3 years
    can this option be added ~/.zshrc somehow?