Dynamically reformatting man pages on terminal dimension changes

manterminal

One common workflow of mine is to open a manual page in a terminal, then another terminal in which to test things. The man page is formatted to the initial dimensions of the first terminal. When I now resize my windows (or have my WM do that for me automatically), there is either a gap to the right of the preformatted page, or lines wrap. At this point I usually q(uit) and !! (run again), which loses my position in the page.

I assume the formatting process is quite CPU intensive, or maybe it stems from ancient times of fixed terminal sizes. The less pager dynamically reacts to terminal resize events, so it should be possible in theory.

I tried perusing man pages, searching the Web, asking on IRC — the whole lot — but couldn't come up with anything.

  • Can I trigger reformatting from within
    or outside of the man utility?

  • Is there a version of the man utility
    that resizes the page dynamically?

  • Is there way to customize some part
    of the formatting/display process to
    make it update on SIGWINCH?

Best Answer

The basic problem is that the formatting is done by one program and the paging is done by another. Even if the formatter were to get a signal that the window size has changed and reformat the text for the new window size, all it can do is feed new text down the pipeline to the pager. There's no way for the pager to know with certainty what position in the new stream corresponds to the position in the old stream it was currently displaying.

What you need is for the pager to be able to do the reformatting. As @Robin Green said, that's HTML.

If you want to use HTML but still work in a terminal, you can tell man(1) to output in HTML and call a text-mode browser to display it.

man -Hlynx man

That will display the man(1) manpage in the lynx text-mode browser. Lynx does not directly respond to window size changes, but you can press ctrl-R and lynx will re-render the page for the new window size.

There are two other text-mode browsers that I know of: links and elinks. You could experiment with those and lynx and determine which give you the best experience for browsing man pages. You may want to use a custom configuration just for man pages and invoke a script that invokes the browser with that specific configuration.

You can put the man options you like into the MANOPT environment variable.

$ export MANOPT=-Hlynx
$ export MANOPT=-Hmanlynx # provide your own wrapper to execute
                          # lynx with a different configuration.

You will need to install the groff package for man to be able to generate HTML.

Related Question