Restrict the number of colums for man (or less)

I use man pages in the console a lot (no GUI), but the wide screen I use really has a lot of columns in text mode. The lines are too long and unpleasant to read.

Is there a way to limit the number of columns used by man (or less, which man uses internally)?

Asked By: fleetingbytes

||

Assuming you are on a Linux system that formats its manuals on the fly, set the COLUMNS or MANWIDTH environment variable to the width of lines that you’d like to have:

COLUMNS=72 man ...

You may want to set MANWIDTH to a sensible value in your shell’s startup file as COLUMNS is used by many other programs besides man:

export MANWIDTH=72

On a system using mandoc, use man with the option -O width=72, where 72 is whatever width you want to use. The mandoc system uses a default width of 78, so getting excessively long lines is usually not an issue; I’m just mentioning it here in case you want to modify the width to something even narrower.

On these systems, you could define an alias for your interactive shell like so:

alias man='man -O width=72'

… or a shell function that overloads the man name:

man () {
    command man -O width=72 "$@"
}

The poor man’s option would be to pass the text through fold before piping it to less:

man ... | fold -s -w 72 | less

This folds the output of man at the last space on or before the 72nd column before less shows the output. fold is a standard Unix utility.

The pipeline could be expressed in a shell function that overloads the man name like so:

man () {
    command man "$@" | fold -s -w 72 | less
}

Albeit not standard, the fmt utility is commonly available and may do a better job than the fold utility of folding long lines. Replace fold -s -w 72 with fmt -w 72 in the pipeline above to try it out.

Answered By: Kusalananda
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.