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