aboutsummaryrefslogtreecommitdiff
path: root/misc/zsh/run-help-nix
diff options
context:
space:
mode:
Diffstat (limited to 'misc/zsh/run-help-nix')
-rw-r--r--misc/zsh/run-help-nix42
1 files changed, 42 insertions, 0 deletions
diff --git a/misc/zsh/run-help-nix b/misc/zsh/run-help-nix
new file mode 100644
index 000000000..f91a304eb
--- /dev/null
+++ b/misc/zsh/run-help-nix
@@ -0,0 +1,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 $?