11

I use Guake terminal emulator a lot. It's the best thing since sliced bred IMO.

But one thing has been bugging me, when I want to read man pages the default width of the output is the width of the terminal windows, which in my case is always full screen so it's a bit difficult to read.

Is there a way I can make the default width of the output of man command a, pleasant to read, 80 characters?

The man page for man has this part:

   MANWIDTH
          If  $MANWIDTH  is set, its value is used as the line length for which manual pages should be formatted.  If it is not set,
          manual pages will be formatted with a line length appropriate to the current terminal (using an ioctl(2) if available, the
          value  of  $COLUMNS,  or  falling  back  to 80 characters if neither is available).  Cat pages will only be saved when the
          default formatting can be used, that is when the terminal line length is between 66 and 80 characters.

But I cant figure out where to change it.

I tried adding the line:

MANWIDTH 80

to /etc/manpath.config and ~/.bashrc, but with no result.

Kok Nikol
  • 237
  • 1
  • 2
  • 8

3 Answers3

16

That's an environment variable.

Try:

MANWIDTH=80
export MANWIDTH
man bash

If you want that set permanently then you can add those first two lines to your shell session startup scripts or similar.

Etan Reisner
  • 68,917
  • 7
  • 78
  • 118
  • I added `export MANWIDTH=80` to the ~/.bashrc and it works. Why doesn't it work without `export`? – Kok Nikol May 11 '15 at 17:34
  • 1
    Without export you are setting it for the shell session itself only. No other processes see it. With `export` you are telling the shell to put it in the process environment so that child processes get the variable set in *their* environments when they start and they can then use the values to do things. `export var=value` and `var=value; export var` are identical except when `value` is a command when using `export var=$(command)` means you can't see the return code of the command you ran (and older shells don't support the `export var=value` format I believe). – Etan Reisner May 11 '15 at 17:44
  • a nice one: in your `~/.bashrc` file, add: `alias manw='MANWIDTH=$COLUMNS man'` # will automagically have a man page fitting better to your own screen, even if you resize it (as long as you resize when on the prompt). use then `manw` instead of `man` – Olivier Dulac Jan 20 '17 at 15:38
5

As pointed out in other answers, setting and exporting MANWIDTH properly is the way to go.

I would avoid hardcoding it, or else it will overflow / have ugly linebreaks when your terminal emulator window is more narrow than that value:

NAME
       grep, egrep, fgrep - print lines that match
 patterns

SYNOPSIS
       grep [OPTION...] PATTERNS [FILE...]
       grep [OPTION...] -e PATTERNS ... [FILE...]
       grep [OPTION...] -f PATTERN_FILE ... [FILE.
..]

DESCRIPTION
       grep  searches  for  PATTERNS  in  each  FI
LE.  PATTERNS is one or more
       patterns separated by newline characters, a
nd  grep  prints  each  line
       that  matches a pattern.  Typically PATTERN
S should be quoted when grep
       is used in a shell command.

Here's what I use, in a handy alias:

alias man='MANWIDTH=$((COLUMNS > 80 ? 80 : COLUMNS)) man'

This sets MANWIDTH to 80 if the terminal window is wider than that, and to COLUMNS (the current width of the terminal window) if it is more narrow.

Result in a wide window:

NAME
       grep, egrep, fgrep - print lines that match patterns

SYNOPSIS
       grep [OPTION...] PATTERNS [FILE...]
       grep [OPTION...] -e PATTERNS ... [FILE...]
       grep [OPTION...] -f PATTERN_FILE ... [FILE...]

DESCRIPTION
       grep  searches  for  PATTERNS  in  each  FILE.  PATTERNS is one or more
       patterns separated by newline characters, and  grep  prints  each  line
       that  matches a pattern.  Typically PATTERNS should be quoted when grep
       is used in a shell command.

Result in a narrow window:

NAME
       grep,  egrep, fgrep - print lines that
       match patterns

SYNOPSIS
       grep [OPTION...] PATTERNS [FILE...]
       grep  [OPTION...]  -e   PATTERNS   ...
       [FILE...]
       grep  [OPTION...]  -f PATTERN_FILE ...
       [FILE...]

DESCRIPTION
       grep searches  for  PATTERNS  in  each
       FILE.    PATTERNS   is   one  or  more
       patterns    separated    by    newline
       characters,  and grep prints each line
       that  matches  a  pattern.   Typically
       PATTERNS should be quoted when grep is
       used in a shell command.
Benjamin W.
  • 33,075
  • 16
  • 78
  • 86
  • I find that this works quite well: `export MANWIDTH="$((COLUMNS - 5))`. Setting the width to be the same as `COLUMNS` still had the effect of cutting off some of the text, and 80 was just too narrow – smac89 Apr 27 '21 at 05:39
4

You need to set this as an environment variable.

MANWIDTH=80 man man

works here, and provides the manpage for man in 80 column glory.

If you want this in .bashrc the correct line entry is

export MANWIDTH=80

Note lack of spaces around = sign. You may or may not need export.

abligh
  • 23,144
  • 3
  • 41
  • 81
  • The `export` was needed. I was close, didn't include the `=` sign -_- It works now. Thanks! – Kok Nikol May 11 '15 at 17:27
  • I didn't know that you can set the env. variable like this: `MANWIDTH=80 man man` that is, in one line, thanks for that too. – Kok Nikol May 11 '15 at 17:28
  • The `VAR=value command` version is a way to set some number of environment variables for the command to be run only (and only for that single run). – Etan Reisner May 11 '15 at 17:46
  • You need the export so that the variable gets passed in the environment to child processes launched by the shell. I had a brain fade and couldn't remember whether that was automatically done for `.bashrc` files (it's not). The one liner version *only* sets the variable for the thing you are running (try `echo $MANWIDTH` after). – abligh May 11 '15 at 19:12