man.plugin.zsh 1.04 KB
Newer Older
Jerry Ling's avatar
Jerry Ling committed
1
2
3
4
5
6
7
# ------------------------------------------------------------------------------
# Author
# ------
#
# * Jerry Ling<jerryling315@gmail.com>
#
# ------------------------------------------------------------------------------
8
# Usage
Jerry Ling's avatar
Jerry Ling committed
9
10
11
12
13
14
15
# -----
#
# man will be inserted before the command
#
# ------------------------------------------------------------------------------

man-command-line() {
16
17
18
    # if there is no command typed, use the last command
    [[ -z "$BUFFER" ]] && zle up-history

19
20
21
22
    # if typed command begins with man, do nothing
    [[ "$BUFFER" = man\ * ]] && return

    # get command and possible subcommand
23
    # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
24
25
26
27
28
29
30
31
32
    local -a args
    args=(${${(Az)BUFFER}[1]} ${${(Az)BUFFER}[2]})

    # check if man page exists for command and first argument
    if man "${args[1]}-${args[2]}" >/dev/null 2>&1; then
        BUFFER="man $args"
    else
        BUFFER="man ${args[1]}"
    fi
Jerry Ling's avatar
Jerry Ling committed
33
}
34

Jerry Ling's avatar
Jerry Ling committed
35
36
37
zle -N man-command-line
# Defined shortcut keys: [Esc]man
bindkey "\e"man man-command-line