Skip to content

Instantly share code, notes, and snippets.

@peterhil
Created July 12, 2015 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhil/ae020185e933e49010dc to your computer and use it in GitHub Desktop.
Save peterhil/ae020185e933e49010dc to your computer and use it in GitHub Desktop.
Grep man pages by sections
#!/usr/bin/env zsh
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 by Peter Hillerström
# License: MIT License
# This can be sourced from zsh dotfiles. If you don't care about other
# functions than `about`, put them inside the about function.
# Output can be piped to GNU `fmt -s` command for nice formatting.
about() {
# Grep manpages by sections
local cmd=$1; shift
man $cmd | grep_sections $* | doublelines
}
striplines () { ruby -ne 'puts $_.strip' }
removehyphens () { perl -pe 's/-$//' }
doublelines () { perl -pe 's/\n/\n\n/' }
null_separate_sections () {
# Read whole input at once with -0777
ruby -0777 -pe 'gsub(/\n\n/, "\0")' | perl -pe 's/\n/ /'
}
grep_sections() {
# Grep input by sections (separated by two newlines)
grep -B8 -A24 $* | striplines | removehyphens | null_separate_sections | \
xargs -0 -n1 echo | \
grep $*
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment