20

If I want to get a brief usage message for a bash builtin, I can use help <builtin> at a command prompt, e.g.

$ help export
export: export [-fn] [name[=value] ...] or export -p
    Set export attribute for shell variables.

    Marks each NAME for automatic export to the environment of subsequently
    executed commands.  If VALUE is supplied, assign VALUE before exporting.

    Options:
      -f        refer to shell functions
      -n        remove the export property from each NAME
      -p        display a list of all exported variables and functions

    An argument of `--' disables further option processing.

    Exit Status:
    Returns success unless an invalid option is given or NAME is invalid.

How can I do this in zsh? I've tried

% export --help
zsh: bad option: -e

and

% help export
zsh: command not found: help

Also the word "help" isn't anywhere in man zshbuiltins.

terdon
  • 234,489
  • 66
  • 447
  • 667
the_velour_fog
  • 11,840
  • 16
  • 64
  • 109

4 Answers4

8

thanks to @don_crissti linking through this Arch wiki documentation.
For some reason the code on the Arch wiki causes this error on invocation

/home/velour/.zshrc:unalias:368: no such hash table element: run-help

zsh --version => zsh 5.1.1 (x86_64-ubuntu-linux-gnu)

so to get it to work, I added the below block to ~/.zshrc, then commented out the alias commands.

autoload -Uz run-help
autoload -Uz run-help-git
autoload -Uz run-help-svn
autoload -Uz run-help-svk
#unalias run-help
#alias help=run-help

and simply invoke with

run-help <builtin>

So now I get

% run-help export

export [ name[=value] ... ]
       The specified names are marked for automatic export to the envi-
       ronment  of subsequently executed commands.  Equivalent to type-
       set -gx.  If a parameter specified does not already exist, it is
       created in the global scope.
the_velour_fog
  • 11,840
  • 16
  • 64
  • 109
0

An alternative to the run-help function above is the zman mentioned in a Reddit comment. It’s part of Zinit, but seems to work independently.

Franklin Yu
  • 1,227
  • 12
  • 23
0

Adding the following to .zshrc worked for me.
(building upon the answer of the_velour_fog)

unalias run-help
alias help=run-help
autoload -Uz run-help

Before run-help was an alias for man.

$ zsh --version
zsh 5.9 (x86_64-apple-darwin18.7.0)
$ zsh
\$ type run-help
run-help is an alias for man
\$ unalias run-help
\$ type run-help
run-help not found
\$ autoload -Uz run-help
\$ type run-help
run-help is an autoload shell function
\$ alias help=run-help
\$ help export
export [ name[=value] ... ]
       The specified names are marked for automatic export to the envi-
       ronment  of subsequently executed commands.  Equivalent to type-
       set -gx.  If a parameter specified does not already exist, it is
       created in the global scope.
-1

They have an own man page:

man zshbuiltins
Mecki
  • 256
  • 2
  • 6
  • And the question says, "*the word "help" isn't anywhere in `man zshbuiltins`*." So this answer adds no value. – Toby Speight Jul 02 '23 at 09:04
  • All the information that `help` would ever provide is found on the man page of `zshbuiltins`. If you want to know the options of `export`, you use `help export` in bash but in zsh you use `man zshbuiltins` and there you get the options for `export`. After all you also use `man ls` and not `help ls`; zsh just treats built-ins like non-builtins as the POSIX standard does not require anything to be built-in. And `export` is found on the man page, help not as zsh simply doesn't know any command named help and POSIX does not require that to exist at all. – Mecki Jul 02 '23 at 16:59