Use Vim as a Pager
22nd April 2023 - Saturday
I wanted to be able to scroll a man page in the terminal with my mouse wheel. While looking for a setting to change I learned that Vim can be used as a pager allowing mouse support and much more.
Inspired by this entry at the Vim Tips Wiki, I decided to set it up for myself, but with a few minor changes. I did all this in WSL with Ubuntu 20.04.6 and Zsh on Windows 11, but it should work as well with any Unix or Linux variant:
~/.zshrc:
export PAGER="/bin/sh -c \"unset PAGER;col -b -x | vim -R \
-c 'set ft=man mouse=a nonumber t_te=' \
-c 'highlight Normal ctermbg=NONE guibg=NONE' \
-c 'map q :q<CR>' \
-c 'map <SPACE> <C-D>' \
-c 'map b <C-U>' \
-c 'nmap K :Man <C-R>=expand(\\\"<cword>\\\")<CR><CR>' -\""
This is very similar to what was documented in the Vim Tips Wiki, but I've made a few changes to suit my needs...
/bin/sh -cexecutes a command with the most basic shell on the system (Bash on most modern systems)col -b -xfilters out reverse line feeds used bymanto show underlined text when outputting directly to the console.vim -Rstarts Vim in read-only mode- I initially replaced this with
nvim, but the key mapping for theqkey doesn't work with Neovim.
- I initially replaced this with
-c 'set ft=man mouse=a nonumber t_te='-c: executes a command in Vim. This linesets a few optionsft=man(a.k.a.filetype=man) enables proper syntax highlighting formanpagesmouse=a: enables mouse support for all editor modesnonumber: turns off the line numbers I usually have enabled for editing codet_te=: disables clearing the screen when Vim exits- From this answer on the Vi and Vim Stack Exchange: vi.stackexchange.com/a/435/6330
-c 'highlight Normal ctermbg=NONE guibg=NONE': This disables the background color that I use in the editor when editing code so that my terminal transparency is still in effect.- The rest of the lines change some key mappings so that it works in a familiar way if you're used to
less(the default pager formanpages)