If you toggle the -N switch within less, it displays line numbers. However, there seems to be an unnecessarily generous amount of padding added, even when the total lines is small. E.g. the output of man less with line numbers enabled is:

      1 LESS(1)
      3 NAME
      4        less - opposite of more
      6 SYNOPSIS
      7        less -?
      8        less --help
      9        less -V
     10        less --version
     11        less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]
    940       Version 487: 25 Oct 2016 

Is there a way to control or reduce that padding to the least amount necessary for the total number of lines?

I know I could go for a programmatic solution (e.g. piping into cut etc), but I'm wondering if there's some sort of switch or configuration parameter I'm ignorant of that controls this behaviour.



This functionality has been promptly added to Less in the form of an extra command line option, --line-num-width=N. The original answer below is valid up to Less version 570, as per the commit history.

Original answer

No, there is not an option to reduce the padding. The padding is done in the line.c file of the source code:

 * Display the line number at the start of each line
 * if the -N option is set.
if (linenums == OPT_ONPLUS)
    char buf[INT_STRLEN_BOUND(pos) + 2];
    int n;

    linenumtoa(linenum, buf);
    n = (int) strlen(buf);
    if (n < MIN_LINENUM_WIDTH)
        n = MIN_LINENUM_WIDTH;
    sprintf(linebuf+curr, "%*s ", n, buf);
    n++;  /* One space after the line number. */
    for (i = 0; i < n; i++)
        attr[curr+i] = AT_BOLD;
    curr += n;
    column += n;
    lmargin += n;

The amount of padding is MIN_LINENUM_WIDTH, defined in the header file less.h as 7, enough to preserve the numbers alignment for files with less than ten million lines. If you find that excessive, you can always change it and recompile.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.