1- # zsh-vi-man.zsh -- Smart man page viewer for zsh vi mode
1+ # zsh-vi-man.zsh -- Smart man page viewer for zsh
22# https://github.com/TunaCuma/zsh-vi-man
33#
4- # Press K in vi normal mode to open the man page for the current command.
5- # If your cursor is on an option (like -r or --recursive), it will jump
6- # directly to that option in the man page.
4+ # Press K in vi normal mode, Ctrl-X k in emacs mode, or Ctrl-K in vi insert mode
5+ # to open the man page for the current command. If your cursor is on an option
6+ # (like -r or --recursive), it will jump directly to that option in the man page.
77#
88# MIT License - Copyright (c) 2025 Tuna Cuma
99
1010# Configuration variables (can be set before sourcing)
11- # ZVM_MAN_KEY: the key to trigger man page lookup (default: K)
11+ # ZVM_MAN_KEY: the key to trigger man page lookup in vi normal mode (default: K)
12+ # ZVM_MAN_KEY_EMACS: the key sequence for emacs mode (default: ^Xk, i.e., Ctrl-X k)
13+ # ZVM_MAN_KEY_INSERT: the key for vi insert mode (default: ^K, i.e., Ctrl-K)
1214# ZVM_MAN_PAGER: the pager to use (default: less)
15+ # ZVM_MAN_ENABLE_EMACS: enable emacs mode binding (default: true)
16+ # ZVM_MAN_ENABLE_INSERT: enable vi insert mode binding (default: true)
1317
1418: ${ZVM_MAN_KEY:= K}
19+ : ${ZVM_MAN_KEY_EMACS:= ' ^Xk' }
20+ : ${ZVM_MAN_KEY_INSERT:= ' ^K' }
1521: ${ZVM_MAN_PAGER:= less}
22+ : ${ZVM_MAN_ENABLE_EMACS:= true}
23+ : ${ZVM_MAN_ENABLE_INSERT:= true}
1624
1725function zvm-man() {
1826 # Get the word at cursor position
@@ -87,7 +95,30 @@ function zvm-man() {
8795zle -N zvm-man
8896
8997function _zvm_man_bind_key() {
90- bindkey -M vicmd " ${ZVM_MAN_KEY} " zvm-man
98+ # Bind in vi normal mode (always enabled)
99+ bindkey -M vicmd " ${ZVM_MAN_KEY} " zvm-man 2> /dev/null
100+
101+ # Handle emacs mode binding
102+ if [[ " ${ZVM_MAN_ENABLE_EMACS} " == true ]]; then
103+ bindkey -M emacs " ${ZVM_MAN_KEY_EMACS} " zvm-man 2> /dev/null
104+ else
105+ # Remove existing binding if disabled
106+ bindkey -M emacs -r " ${ZVM_MAN_KEY_EMACS} " 2> /dev/null
107+ fi
108+
109+ # Handle vi insert mode binding
110+ if [[ " ${ZVM_MAN_ENABLE_INSERT} " == true ]]; then
111+ bindkey -M viins " ${ZVM_MAN_KEY_INSERT} " zvm-man 2> /dev/null
112+ else
113+ # Remove existing binding if disabled
114+ bindkey -M viins -r " ${ZVM_MAN_KEY_INSERT} " 2> /dev/null
115+ fi
116+ }
117+
118+ # Expose function so users can manually rebind if needed
119+ # Usage: zvm_man_rebind (if keybindings don't work after sourcing)
120+ function zvm_man_rebind() {
121+ _zvm_man_bind_key
91122}
92123
93124# Support both immediate binding and lazy loading with zsh-vi-mode
@@ -102,7 +133,7 @@ elif (( ${+functions[zvm_after_init]} )); then
102133 # zsh-vi-mode without lazy keybindings
103134 zvm_after_init_commands+=(_zvm_man_bind_key)
104135else
105- # Standalone or other vi-mode setups
136+ # Standalone or other vi-mode setups - bind immediately
106137 _zvm_man_bind_key
107138fi
108139
0 commit comments