Skip to content

Instantly share code, notes, and snippets.

@j0yu
Last active May 29, 2020 11:39
Show Gist options
  • Select an option

  • Save j0yu/cfff2ebb74bb7e941eef0286a401aa6e to your computer and use it in GitHub Desktop.

Select an option

Save j0yu/cfff2ebb74bb7e941eef0286a401aa6e to your computer and use it in GitHub Desktop.
man page autocomplete

Basic bash completion for man command.

  • Known manual page names
  • man options

More could be added especially for the options for each flag but I rarely use them.

Instructions

Either have this inside your ~/.bashrc or save it somewhere and source it in your ~/.bashrc

#!/bin/bash

_man ()
{
  local CWORD
  CWORD="${COMP_WORDS[COMP_CWORD]}"
  # printf 'current word "%s"\n' "$CWORD"  # For debugging
  case "$CWORD" in
    -* ) readarray -t COMPREPLY < <(man --help | grep -oP '\-[\w?\-]+');;
    *) readarray -t COMPREPLY < <(man -k . | sed -n "/^$CWORD/ s/ .*//p");;
  esac
}
complete -F _man man
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment