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...