mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
translated by perfifer
This commit is contained in:
parent
5656196381
commit
23f35cd8f7
@ -1,94 +0,0 @@
|
||||
[#]: subject: "How I use the Linux fmt command to format text"
|
||||
[#]: via: "https://opensource.com/article/22/7/fmt-trivial-text-formatter"
|
||||
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "perfiffer"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
How I use the Linux fmt command to format text
|
||||
======
|
||||
The fmt command is a trivial text formatter. Here's how I use it to format text and email replies.
|
||||
|
||||
When I write documentation for a project, I often write the Readme file and Install instructions in plain text. I don't need to use markup languages like HTML or Markdown to describe what a project does or how to compile it. But maintaining this documentation can be a pain. If I need to update the middle of a sentence in my `Readme` text file, I need to reformat the text so I don't end up with a really long or short line in the middle of my other text that's otherwise formatted to 75 columns. Some editors include a feature that will automatically reformat text to fill paragraphs, but not all do. That's where the Linux `fmt` command comes to the rescue.
|
||||
|
||||
### Format text with Linux fmt
|
||||
|
||||
The `fmt` command is a trivial text formatter; it collects words and fills paragraphs, but doesn't apply any other text styling such as italics or bold. It's all just plain text. With `fmt`, you can quickly adjust text so it's easier to read. Let's say I start with this familiar sample text:
|
||||
|
||||
```
|
||||
$ cat trek.txt
|
||||
Space: the final
|
||||
frontier. These are the voyages
|
||||
of the starship Enterprise. Its
|
||||
continuing mission: to explore
|
||||
strange new worlds. To
|
||||
seek out new life and new
|
||||
civilizations. To boldly go
|
||||
where no one has gone before!
|
||||
```
|
||||
|
||||
In this sample file, lines have different lengths, and they are broken up in an odd way. You might have similar odd line breaks if you make lots of changes to a plain text file. To reformat this text, you can use the `fmt` command to fill the lines of the paragraph to a uniform length:
|
||||
|
||||
```
|
||||
$ fmt trek.txt
|
||||
Space: the final frontier. These are the voyages of the starship
|
||||
Enterprise. Its continuing mission: to explore strange new worlds. To
|
||||
seek out new life and new civilizations. To boldly go where no one has
|
||||
gone before!
|
||||
```
|
||||
|
||||
By default, `fmt` will format text to 75 columns wide, but you can change that with the -w or --width option:
|
||||
|
||||
```
|
||||
$ fmt -w 60 trek.txt
|
||||
Space: the final frontier. These are the voyages of
|
||||
the starship Enterprise. Its continuing mission: to
|
||||
explore strange new worlds. To seek out new life and new
|
||||
civilizations. To boldly go where no one has gone before!
|
||||
```
|
||||
|
||||
### Format email replies with Linux fmt
|
||||
|
||||
I participate in an email list where we prefer plain text emails. That makes archiving emails on the list server much easier. But the reality is not everyone sends emails in plain text. And sometimes, when I reply to those emails as plain text, my email client puts an entire paragraph on one line. That makes it difficult to "quote" a reply in an email.
|
||||
|
||||
Here's a simple example. When I'm replying to an email as plain text, my email client "quotes" the other person's email by adding a > character before each line. For a short message, that might look like this:
|
||||
|
||||
```
|
||||
> I like the idea of the interim development builds.
|
||||
```
|
||||
|
||||
A long line that doesn't get "wrapped" properly will not display correctly in my plain text email reply, because it will be just one long line with a > character at the front, like this:
|
||||
|
||||
```
|
||||
> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.
|
||||
```
|
||||
|
||||
To fix this, I bring up a terminal and copy and paste the quoted text into a new file. Then I use the -p or --prefix option to tell `fmt` what character to use as a "prefix" before each line.
|
||||
|
||||
```
|
||||
$ cat > email.txt
|
||||
> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.
|
||||
^D
|
||||
$ fmt -p '>' email.txt
|
||||
> I like the idea of the interim development builds. This should be a
|
||||
> great way to test new changes that everyone can experiment with.
|
||||
```
|
||||
|
||||
The`fmt` command is a very simple text formatter, but it can do lots of useful things that help in writing and updating documentation in plain text. Explore the other options such as -c or --crown-margin to match the indentation of the first two lines of a paragraph, such as bullet lists. Also try -t or --tagged-paragraph to preserve the indentation of the first line in a paragraph, like indented paragraphs. And the -u or --uniform-spacing option to use one space between words and two spaces between sentences.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/7/fmt-trivial-text-formatter
|
||||
|
||||
作者:[Jim Hall][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/jim-hall
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/sites/default/files/lead-images/osdc-docdish-typewriterkeys-3-series.png
|
@ -0,0 +1,96 @@
|
||||
[#]: subject: "How I use the Linux fmt command to format text"
|
||||
[#]: via: "https://opensource.com/article/22/7/fmt-trivial-text-formatter"
|
||||
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "perfiffer"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
我是如何使用 Linux fmt 命令来格式化文本
|
||||
|
||||
======
|
||||
fmt 命令是一个简单的文本格式化程序。我将在这里展示如何使用它来格式化文本和邮件回复。
|
||||
|
||||
当我为项目编写文档时,我经常以纯文本的形式编写自述文件和安装说明。我不需要使用 HTML 或者 Markdown 之类的标记语言来描述项目的功能或如何编译它。但是维护此文档可能会很痛苦。如果我需要在我的 `Readme` 文件中更新一个句子的中间位置,我需要重新格式化文本,这样我就不会在我的其它文本中间出现一个很长或很短的行,否则它会被格式化为 75 列。一些编辑器包含可以自动重新格式化文本以填充段落的功能,但并非所有的编辑器都这样做。这就是 Linux `fmt` 命令的用武之地。
|
||||
|
||||
### 使用 Linux fmt 命令格式化文本
|
||||
|
||||
`fmt` 命令是一个简单的文本格式化程序;它收集单词并填充段落,但不应用任何其它文本样式,例如斜体或粗体。这一切都是纯文本。使用 `fmt` 命令,你可以快速调整文本,使其更易于阅读。让我们从这个熟悉的示例文本开始:
|
||||
|
||||
```
|
||||
$ cat trek.txt
|
||||
Space: the final
|
||||
frontier. These are the voyages
|
||||
of the starship Enterprise. Its
|
||||
continuing mission: to explore
|
||||
strange new worlds. To
|
||||
seek out new life and new
|
||||
civilizations. To boldly go
|
||||
where no one has gone before!
|
||||
```
|
||||
|
||||
在这个实例文件中,每行都有不同的长度,并且它们以一种奇怪的方式被分割。如果你对纯文本文件进行大量更改,你可以会遇到类似的奇怪的换行。要重新格式化此文本,你可以使用 `fmt` 命令将段落的行填充为统一长度:
|
||||
|
||||
|
||||
```
|
||||
$ fmt trek.txt
|
||||
Space: the final frontier. These are the voyages of the starship
|
||||
Enterprise. Its continuing mission: to explore strange new worlds. To
|
||||
seek out new life and new civilizations. To boldly go where no one has
|
||||
gone before!
|
||||
```
|
||||
|
||||
默认情况下,`fmt` 会将文本格式化为 75 的列宽大小,但你可以使用 `-w` 或 `--width` 选项进行更改:
|
||||
|
||||
```
|
||||
$ fmt -w 60 trek.txt
|
||||
Space: the final frontier. These are the voyages of
|
||||
the starship Enterprise. Its continuing mission: to
|
||||
explore strange new worlds. To seek out new life and new
|
||||
civilizations. To boldly go where no one has gone before!
|
||||
```
|
||||
|
||||
### 使用 Linux fmt 命令格式化电子邮件回复
|
||||
|
||||
我参与了一个邮件列表,我们更喜欢纯文本电子邮件。这使得在列表服务器上归档电子邮件变得更加容易。但现实并非每个人都以纯文本形式发送电子邮件。有时候,当我以纯文本形式回复这些电子邮件时,我的电子邮件客户端会将整个段落放在一行中。这使得在电子邮件中“引用”回复变得困难。
|
||||
|
||||
这是一个简单的例子。当我以纯文本形式回复电子邮件时,我的电子邮件客户端通过在每行前添加 `>` 字符来”引用“对方的电子邮件。对于一条短消息,可能如下所示:
|
||||
|
||||
```
|
||||
> I like the idea of the interim development builds.
|
||||
```
|
||||
|
||||
没有正确“换行”的长行将无法在我的纯文本电子邮件回复中正确显示,因为它只是前面带有 `>` 字符的长行,如下所示:
|
||||
|
||||
```
|
||||
> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.
|
||||
```
|
||||
|
||||
为了解决这个问题,我打开了一个终端并将引用的文本复制并粘贴到一个新文件中。然后我使用 `-p` 或 `--prefix` 选项来告诉 `fmt` 在每一行之前使用什么字符作为“前缀”。
|
||||
|
||||
```
|
||||
$ cat > email.txt
|
||||
> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.
|
||||
^D
|
||||
$ fmt -p '>' email.txt
|
||||
> I like the idea of the interim development builds. This should be a
|
||||
> great way to test new changes that everyone can experiment with.
|
||||
```
|
||||
|
||||
`fmt` 命令是一个非常简单的文本格式化程序,但它可以做很多有用的事情,有助于以纯文本形式编写和更新文档。探索其它选项,例如 `-c` 或 `--crown-margin` 以匹配段落前两行缩进,例如项目符合列表。还可以尝试使用 `-t` 或者 `--tagged-paragraph` 来保留段落中第一行的缩进,就像缩进的段落一样。`-u` 或 `--uniform-spacing` 选项在单词之间使用一个空格,在句子之间使用两个空格。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/22/7/fmt-trivial-text-formatter
|
||||
|
||||
作者:[Jim Hall][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/perfiffer)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/jim-hall
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://opensource.com/sites/default/files/lead-images/osdc-docdish-typewriterkeys-3-series.png
|
Loading…
Reference in New Issue
Block a user