manpager.vim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. " Vim plugin for using Vim as manpager.
  2. " Maintainer: Enno Nagel <ennonagel+vim@gmail.com>
  3. " Last Change: 2022 Oct 17
  4. if exists('g:loaded_manpager_plugin')
  5. finish
  6. endif
  7. let g:loaded_manpager_plugin = 1
  8. " Set up the current buffer (likely read from stdin) as a manpage
  9. command MANPAGER call s:ManPager()
  10. function s:ManPager()
  11. " global options, keep these to a minimum to avoid side effects
  12. if &compatible
  13. set nocompatible
  14. endif
  15. if exists('+viminfofile')
  16. set viminfofile=NONE
  17. endif
  18. syntax on
  19. " Make this an unlisted, readonly scratch buffer
  20. setlocal buftype=nofile noswapfile bufhidden=hide nobuflisted readonly
  21. " Ensure text width matches window width
  22. setlocal foldcolumn& nofoldenable nonumber norelativenumber
  23. " In case Vim was invoked with -M
  24. setlocal modifiable
  25. " Emulate 'col -b'
  26. silent! keepj keepp %s/\v(.)\b\ze\1?//ge
  27. " Remove ansi sequences
  28. silent! keepj keepp %s/\v\e\[%(%(\d;)?\d{1,2})?[mK]//ge
  29. " Remove empty lines above the header
  30. call cursor(1, 1)
  31. let n = search(".*(.*)", "c")
  32. if n > 1
  33. exe "1," . n-1 . "d"
  34. endif
  35. " Finished preprocessing the buffer, prevent any further modifications
  36. setlocal nomodified nomodifiable
  37. " Set filetype to man even if ftplugin is disabled
  38. setlocal filetype=man
  39. runtime ftplugin/man.vim
  40. endfunction