我最喜欢的一个关于Unix的故事是它如何变成了一个文本处理系统。Brian Kernighan在他的《Unix:A History and a Memoir(第3章)中讲述了这个故事,但要总结一下。贝尔实验室的Unix团队在一台PDP-7计算机上运行最初的Unix,但这是一个很小的系统,没有足够的资源来支持新的工作。所以肯-汤普森和其他人游说购买一台新的PDP-11计算机。管理层拒绝了这一请求。大约在同一时间,专利部门计划购买一个新的计算机平台,以使用专利文件格式化软件制作专利申请。Unix小组建议专利部门为Unix小组购买一台新的PDP-11,而Unix小组将为他们创建格式化软件。
这个新的格式化系统被称为nroff ,是 "新罗夫 "的简称,是20世纪60年代计算机系统中一个名为罗夫的文本格式化程序的更新版本。罗夫这个名字来自于一个古老的说法:"我会跑掉一个文件"。
用nroff进行基本格式化
默认情况下,nroff ,收集单词并填充段落。当nroff 遇到空行时,它会开始一个新的段落。例如,从这篇文章的导言开始,它只有几段长。
$ cat intro
Old-school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned
into a text processing system. Brian Kernighan tells the
story in his book Unix: A History and a Memoir (chapter 3)
but to summarize:
The Unix team at Bell Labs ran the original Unix on
a PDP-7 computer, but it was a tiny system and didn't
have sufficient resources to support new work. So Ken
Thompson and others lobbied to purchase a new PDP-11
computer. Management denied the request. Around the same
time, the Patents department planned to buy a new computer
platform to produce patent applications using proprietary
document formatting software. The Unix group proposed
that the Patents department instead buy a new PDP-11 for
the Unix team, and the Unix team would create formatting
software for them.
That new formatting system was called nroff, short for
"New Roff," an updated version of a text formatting program
called Roff from a 1960s computer system. The name Roff
came from the old expression, "I'll run off a document."
如果你用nroff 来处理这个文件,各行就会被 "粘 "在一起,所以输出的是带有完整对齐的段落。如果使用nroff ,还可以将单词连在一起,这有助于平衡文本中的行数。
$ nroff intro | head
Old‐school technical writing with groff Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
Around the same time, the Patents department planned to buy a new
原始的Unix系统使用打字机式的打印机,在一个US Letter页面上使用66行80列,而nroff 也做了同样的假设。它还增加了空行,所以每页的输出是66行,但我用head 命令只显示输出的前几行,因为我的样本文本不是很长。
分割行和文本居中
前两行本应是独立的文本行。你可以插入一个格式化指令,告诉nroff ,以增加一个换行符。所有nroff 指令都以一个点开始,后面是一个简短的命令。要添加一个换行符,请在第一行和第二行之间使用.br 指令。
Old-school technical writing with groff
.br
Jim Hall
当你处理这个新文件时,nroff 将标题和作者打印在不同的行上。
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
你可以添加其他格式,使这个文件看起来更好。要使顶部两行居中,请使用.ce 格式化请求。这需要一个数字参数,以表明nroff 应该居中多少行。在这里,你可以用.ce 2 请求将最上面的两行输出居中。
.ce 2
Old-school technical writing with groff
.br
Jim Hall
有了这个附加指令,nroff 可以正确地将前两行居中。
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
添加页边距
在打印机上打印的结果是文本从页面的第一行开始,并且靠左边缘。要想从页面顶部增加几行额外的空间,请使用.sp ,并注明要增加的空白行数。
.sp 5
.ce 2
Old-school technical writing with groff
.br
Jim Hall
默认情况下,nroff 格式化输出,因此每行都是65列宽。打印到一个80列的美国信纸页面,会留下15个空栏。在左边添加7个空格,可以整齐地平衡输出,左右页边距相等。你可以使用.po 7 请求创建这个页面偏移。
.po 7
.sp 5
.ce 2
Old-school technical writing with groff
.br
Jim Hall
用nroff 处理新的文件,产生一个可供打印的纯文本页面。
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
打印到激光打印机
后来,贝尔实验室的Unix团队获得了一台照排机,能够产生类似于激光打印机的打印文本。为了支持排字机的新功能,Unix团队更新了nroff ,成为排字机专用的troff 程序,几年后再次更新,成为ditroff ,即独立于设备的troff 版本。
Linux系统使用GNUgroff 程序提供现代版本的nroff 和troff 。你仍然可以使用旧的nroff 程序名称来生成纯文本输出,或者使用troff 来生成ditroff兼容输出。使用groff 程序,你还可以为其他种类的输出文件准备文件,如Postscript。
你可以用groff来处理同一个输入文件,通过使用-T 选项选择合适的输出类型,在兼容Postscript的激光打印机上打印,如-Tps 来生成Postscript文件。例如,我可以用lpr 命令和HP_LaserJet_CP1525nw 设备向打印机打印,因为我的Linux系统就是这样识别我的激光打印机的。
$ groff -Tps intro | lpr -P "HP_LaserJet_CP1525nw"
生成其他类型的输出
如果你反而想把输出保存为PDF文件,你可以用ps2pdf 工具转换Postscript。
$ groff -Tps intro | ps2pdf - > intro.pdf
要从同一个文件生成一个网页,使用-Thtml ,将输出类型设置为HTML。
$ groff -Thtml intro > index.html
groff 命令支持很多其他内置的格式化请求,以提供其他种类的文档格式。
使用这些内置的命令对文档进行格式化,需要花费大量的精力来保持一切看起来都是一样的。使用groff 的技术撰稿人则使用一个叫做宏的格式化请求集合,它提供了自己的命令来生成章节标题、段落、方块引号、脚注、列表和其他有用的文档格式化。