aboutsummaryrefslogtreecommitdiff
path: root/misc/zsh/run-help-nix
blob: f91a304eb3806284aabcfdb9fc75ed6e43b0225b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
emulate -L zsh

# run-help is a zsh widget that can be bound to a key. It mainly looks up the
# man page for the currently typed in command.
#
# Although run-help works for any command without requiring special support,
# it can only deduce the right man page based solely on the name of the
# command. Programs like Nix provide better integration with run-help by
# helping zsh identify Nix subcommands and their corresponding man pages. This
# is what this function does.
#
# To actually use run-help on zsh, place the following lines in your .zshrc:
#
#     (( $+aliases[run-help] )) && unalias run-help
#     autoload -Uz run-help run-help-nix
#
# Then also assign run-help to any key of choice:
#
#     bindkey '^[h' run-help

while [[ "$#" != 0 && "$1" == -* ]]; do
  shift
done

local -a subcommands; subcommands=( nix3 )

local arg
for arg in "$@"; do
  if man -w "${(j:-:)subcommands}-$arg" >/dev/null 2>&1; then
    subcommands+="$arg"
  else
    break
  fi
done

if (( $#subcommands > 1 )); then
  man "${(j:-:)subcommands}"
else
  man nix
fi

return $?