如果您在-N
内切换开关less
,它会显示行号。然而,似乎添加了大量不必要的填充,即使总行数很小。例如,man less
启用行号的输出是:
1 LESS(1)
2
3 NAME
4 less - opposite of more
5
6 SYNOPSIS
7 less -?
8 less --help
9 less -V
10 less --version
11 less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]
...
940 Version 487: 25 Oct 2016
Run Code Online (Sandbox Code Playgroud)
有没有办法控制或减少填充到总行数所需的最少数量?
我知道我可以寻求一个程序化的解决方案(例如管道进入cut
等),但我想知道是否有某种我不知道的开关或配置参数来控制这种行为。
更新
此功能已被及时加入到少一个额外的命令行选项的形式,--line-num-width=N
。根据提交历史记录,下面的原始答案在 Less 版本 570 之前有效。
原答案
不,没有减少填充的选项。填充是在源代码的line.c
文件中完成的:
/*
* 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;
}
Run Code Online (Sandbox Code Playgroud)
填充量是MIN_LINENUM_WIDTH
,在头文件中定义less.h
为 7,足以为少于一千万行的文件保留数字对齐。如果您发现过多,您可以随时更改并重新编译。