Limiting the output of the ‘man’ command to a max number of columns with an alias

It is hard to read text if the columns are too wide. If i have a wide terminal window open I don’t want e.g. man pages text to fill the entire width of the window (I also use a “readability” greasemonkey/tampermonkey-script to apply custom formatting to Wikipedia pages (slightly customized version of https://userstyles.org/styles/56739/wikipedia-readable-hyphenation┬áthat fixes ToC numbering)).

I thought that I could create an alias that set the max number of columns for man to 120 pretty easily.

The environment variable MANWIDTH can be used to control the number of columns manwill format its output to. But one cannot simply set this to 120 and be done with it because then it will format to 120 columns also when the terminal window is narrower, which usually makes terminals wrap at the right edge and then again at a total of 120 char width:

So, my first solution was to first get the width of the terminal window in columns, then use that if the window was narrower than 120 else 120. Something that I found a lot trickier than expected (I hate shell scripting, always seem to only get those parenthesis and quotation marks right by dumb luck after lots of trial and error).

But when revisiting it a good while later I simplified it a tad by just not setting MANWIDTH if width was lower than 120 (duh). The result:

# Attempt 1
alias man='MANWIDTH=$(((x=$(tput cols))) && [ $x -gt 120 ] && echo 120 || echo $x)

# Simplified, duh
alias man='MANWIDTH=$([ $(tput cols) -lt 120 ] || echo 120) man'

tput colswill get current terminal width reliably.

Results: