vimpager 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/sh
  2. # Script for using ViM as a PAGER.
  3. # Based on Bram's less.sh.
  4. # Version 1.4
  5. # git://github.com/rkitover/vimpager.git
  6. file="$@"
  7. if [ -z "$file" ]; then file="-"; fi
  8. if uname -s | grep -iq cygwin; then
  9. cygwin=1
  10. elif uname -s | grep -iq linux; then
  11. linux=1
  12. elif uname -s | grep -iq sunos; then
  13. solaris=1
  14. else
  15. bsd=1
  16. fi
  17. less_vim() {
  18. vim -R \
  19. -c 'let no_plugin_maps = 1' \
  20. -c 'set scrolloff=999' \
  21. -c 'runtime! macros/less.vim' \
  22. -c 'set foldlevel=999' \
  23. -c 'set mouse=h' \
  24. -c 'set nonu' \
  25. -c 'nmap <ESC>u :nohlsearch<cr>' \
  26. "$@"
  27. }
  28. do_ps() {
  29. if [ $solaris ]; then
  30. ps -u `id -u` -o pid,comm=
  31. elif [ $bsd ]; then
  32. ps -U `id -u` -o pid,comm=
  33. else
  34. ps fuxw
  35. fi
  36. }
  37. pproc() {
  38. if [ $linux ]; then
  39. ps -p $1 -o comm=
  40. elif [ $cygwin ]; then
  41. ps -p $1 | sed -e 's/^I/ /' | grep -v PID
  42. else
  43. ps -p $1 -o comm= | grep -v PID
  44. fi
  45. }
  46. ppid() {
  47. if [ $linux ]; then
  48. ps -p $1 -o ppid=
  49. elif [ $cygwin ]; then
  50. ps -p $1 | sed -e 's/^I/ /' | grep -v PID | awk '{print $2}'
  51. else
  52. ps -p $1 -o ppid= | grep -v PID
  53. fi
  54. }
  55. # Check if called from man, perldoc or pydoc
  56. if do_ps | grep -q '\(py\(thon\|doc\|doc2\)\|man\|perl\(doc\)\?\([0-9.]*\)\?\)\>'; then
  57. proc=$$
  58. while next_parent=`ppid $proc` && [ $next_parent != 1 ]; do
  59. if pproc $next_parent | grep -q 'man\>'; then
  60. cat $file | sed -e 's/\[[^m]*m//g' | sed -e 's/.//g' | less_vim -c 'set ft=man' -; exit
  61. elif pproc $next_parent | grep -q 'py\(thon\|doc\|doc2\)\>'; then
  62. cat $file | sed -e 's/\[[^m]*m//g' | sed -e 's/.//g' | less_vim -c 'set ft=man' -; exit
  63. elif pproc $next_parent | grep -q 'perl\(doc\)\?\([0-9.]*\)\?\>'; then
  64. cat $file | sed -e 's/.//g' | less_vim -c 'set ft=man' -; exit
  65. fi
  66. proc=$next_parent
  67. done
  68. fi
  69. less_vim "$file"
  70. # CONTRIBUTORS:
  71. #
  72. # Rafael Kitover
  73. # Antonio Ospite
  74. # Jean-Marie Gaillourdet
  75. # Perry Hargrave
  76. # Koen Smits
  77. # Ivan S. Freitas <ivansichfreitas@gmail.com>