/bash_completion.d/man

http://github.com/brinkman83/bashrc · #! · 83 lines · 69 code · 14 blank · 0 comment · 0 complexity · 4692850fbf6e1f245ec4dbe062e5ebef MD5 · raw file

  1. # man(1) completion
  2. [ $USERLAND = GNU -o $UNAME = Darwin \
  3. -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
  4. -o $UNAME = OpenBSD ] &&
  5. _man()
  6. {
  7. local cur i prev sect manpath manext mansect uname
  8. manext="@([0-9lnp]|[0-9][px]|man|3pm)?(.@(gz|bz2|lzma))"
  9. mansect="@([0-9lnp]|[0-9][px]|3pm)"
  10. COMPREPLY=()
  11. cur=`_get_cword`
  12. prev=${COMP_WORDS[COMP_CWORD-1]}
  13. if [[ "$prev" == -l ]]; then
  14. _filedir $manext
  15. return 0
  16. fi
  17. _expand || return 0
  18. # file based completion if parameter contains /
  19. if [[ "$cur" == */* ]]; then
  20. _filedir $manext
  21. return 0
  22. fi
  23. uname=$( uname -s )
  24. if [[ $uname == @(Linux|GNU|GNU/*|FreeBSD|Cygwin|CYGWIN_*) ]]; then
  25. manpath=$( manpath 2>/dev/null || command man --path )
  26. else
  27. manpath=$MANPATH
  28. fi
  29. if [ -z "$manpath" ]; then
  30. COMPREPLY=( $( compgen -c -- "$cur" ) )
  31. return 0
  32. fi
  33. # determine manual section to search
  34. [[ "$prev" == $mansect ]] && sect=$prev || sect='*'
  35. manpath=$manpath:
  36. if [ -n "$cur" ]; then
  37. manpath="${manpath//://*man$sect/$cur* } ${manpath//://*cat$sect/$cur* }"
  38. else
  39. manpath="${manpath//://*man$sect/ } ${manpath//://*cat$sect/ }"
  40. fi
  41. # redirect stderr for when path doesn't exist
  42. COMPREPLY=( $( eval command ls "$manpath" 2>/dev/null ) )
  43. # weed out directory path names and paths to man pages
  44. COMPREPLY=( ${COMPREPLY[@]##*/?(:)} )
  45. # strip suffix from man pages
  46. COMPREPLY=( ${COMPREPLY[@]%.@(gz|bz2|lzma)} )
  47. COMPREPLY=( $( compgen -W '${COMPREPLY[@]%.*}' -- "${cur//\\\\/}" ) )
  48. if [[ "$prev" != $mansect ]]; then
  49. # File based completion for the rest, prepending ./ if needed
  50. # (man 1.6f needs that for man pages in current dir)
  51. local start=${#COMPREPLY[@]}
  52. _filedir $manext
  53. for (( i=$start; i < ${#COMPREPLY[@]}; i++ )); do
  54. [[ ${COMPREPLY[i]} == */* ]] || COMPREPLY[i]=./${COMPREPLY[i]}
  55. done
  56. fi
  57. return 0
  58. }
  59. [ $USERLAND = GNU -o $UNAME = Darwin \
  60. -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
  61. -o $UNAME = OpenBSD ] && \
  62. complete -F _man $filenames man apropos whatis
  63. # Local variables:
  64. # mode: shell-script
  65. # sh-basic-offset: 4
  66. # sh-indent-comment: t
  67. # indent-tabs-mode: nil
  68. # End:
  69. # ex: ts=4 sw=4 et filetype=sh