How can I add line numbers to man pages or info pages in Linux? I want to use line numbers to navigate in man pages. I can write the man page in a file and then open it with Vim, but is there a better way?

  • 4
    FWIW, you can use Vim as the pager for man
    – muru
    Jan 22 at 11:54
  • 1
    It is worth noting that line numbers of a formatted man page will not be consistent, because part of the formatting man does is to fold text to the correct width for your terminal. You can use them to navigate within a man page once opened, but should not rely on them being the same across invocations of man. Jan 23 at 16:32

4 Answers 4


From man man:

   -P pager, --pager=pager
          Specify  which  output  pager to use.  By default, man uses less
          -s.  This option overrides the $MANPAGER  environment  variable,
          which  in turn overrides the $PAGER environment variable.  It is
          not used in conjunction with -f or -k.

          The value may be a simple command name or a command  with  argu-
          ments, and may use shell quoting (backslashes, single quotes, or
          double quotes).  It may not use pipes to connect  multiple  com-
          mands;  if  you  need that, use a wrapper script, which may take
          the file to display either as an argument or on standard input.

So you could specify the pager to be less with the -N flag for line numbering.

man -P "less -N" 

The problem is that man sets the width of the pages according to the COLUMNS number of your terminal, but the line number for less adds additional characters to the beginning of the line (spaces + the line number), so the format of the output would be a little messed up.

$ man -P "less -N" man


      1 MAN(1)                        Manual pager utils                        M
      1 AN(1)
      5 NAME
      6        man - an interface to the on-line reference manuals
      8 SYNOPSIS
      9        man  [-C  file]  [-d]  [-D]  [--warnings[=warnings]]  [-R encoding
      9 ] [-L
     10        locale] [-m system[,...]] [-M path] [-S list]  [-e  extension]  [-
     10 i|-I]
     11        [--regex|--wildcard]   [--names-only]  [-a]  [-u]  [--no-subpages]
     11   [-P
     12        pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-jus
     12 tifi-
     13        cation]  [-p  string]  [-t]  [-T[device]]  [-H[browser]] [-X[dpi]]
     13  [-Z]
     14        [[section] page ...] ...

So you could change the MANWIDTH environment variables to substract 7 characters (the width of the line prefix for less) from the number of COLUMNS to force man format the pages with shorter width.

$ MANWIDTH=$(( $COLUMNS -7 )) man -P "less -N" man


      1 MAN(1)                     Manual pager utils                     MAN(1)
      5 NAME
      6        man - an interface to the on-line reference manuals
      8 SYNOPSIS
      9        man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L
     10        locale] [-m system[,...]] [-M  path]  [-S  list]  [-e  extension]
     11        [-i|-I]  [--regex|--wildcard] [--names-only] [-a] [-u] [--no-sub-
     12        pages] [-P pager] [-r prompt] [-7]  [-E  encoding]  [--no-hyphen-
     13        ation]   [--no-justification]   [-p   string]  [-t]  [-T[device]]
     14        [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
  • Or call less with -S; that prevents line breaks which mess up the layout. Instead, the parts of the line which do not fit are "to the right", and less scrolls horizontally with the right and left cursor keys Jan 24 at 22:47

Open a manpage, Hit -N then Enter. ( -, then ShiftN, then Enter)

e,g: man man:

  1 MAN(1)                                             Manual pager utils                                             MA      2 
  3 NAME
  4        man - an interface to the system reference manuals
  7        man [man options] [[section] page ...] ...
  8        man -k [apropos options] regexp ...
  9        man -K [man options] [section] term ...
 10        man -f [whatis options] page ...
 11        man -l [man options] file ...
 12        man -w|-W [man options] page ...

To remove line numbers -n Enter

To avoid duplicate lines, set the MANWIDTH variable. LESS variable set to -N to print line numbers:

MANWIDTH=100 LESS=-N man man
  • 2
    My man formats the output to fill the entire screen width, so the following -N operation makes most of the lines wrap. Preceding the command like COLUMNS=$(( COLUMNS - 10 )) man ... makes it leave space for the line numbers. BTW, where did your line 2 go ? I get duplicated line numbers where line wraps occur. Jan 22 at 12:17

man outputs long lines and assumes you have a pager like less.

Note that the line numbers in the output of a specific man page can vary, because man breaks lines according to the window width you run it in.

Try something like:

COLUMNS=72 man --pager='cat' -s 3 printf | nl -ba > Man.printf

Using the fold command on the output is ugly, because man uses the COLUMNS variable to space the lines even though it is not writing to the tty, and fold then reworks all the word spacing to single spaces.

You probably want to wrap this in a script to make the -s 3 section number, and the page name, into arguments.

  • 2
    Why not --pager='cat -n'? That way, you don't need to pipe.
    – terdon
    Jan 23 at 14:29
  • @terdon Forgot -n. In the same vein, --pager='nl -bt -s " | " -l2 would let you use a wider range of numbering options, also without the pipe. Jan 24 at 10:25

Others have given working solutions, so I'd like to just add an alternative:

Install the following 2 packages:

The latter will install an application called batman, which will not only colorize your man pages, but also add line numbers.

Here's an example of how the output will look like:

enter image description here

(The command I entered was simply batman emerge)

As you can see, batman handles display width automatically for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .