man.plugin.zsh
1064 bytes
1# ------------------------------------------------------------------------------
2# Author
3# ------
4#
5# * Jerry Ling<jerryling315@gmail.com>
6#
7# ------------------------------------------------------------------------------
8# Usage
9# -----
10#
11# man will be inserted before the command
12#
13# ------------------------------------------------------------------------------
14
15man-command-line() {
16 # if there is no command typed, use the last command
17 [[ -z "$BUFFER" ]] && zle up-history
18
19 # if typed command begins with man, do nothing
20 [[ "$BUFFER" = man\ * ]] && return
21
22 # get command and possible subcommand
23 # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
24 local -a args
25 args=(${${(Az)BUFFER}[1]} ${${(Az)BUFFER}[2]})
26
27 # check if man page exists for command and first argument
28 if man "${args[1]}-${args[2]}" >/dev/null 2>&1; then
29 BUFFER="man $args"
30 else
31 BUFFER="man ${args[1]}"
32 fi
33}
34
35zle -N man-command-line
36# Defined shortcut keys: [Esc]man
37bindkey "\e"man man-command-line