Linux 的 fmt 命令用法与案例
fmt 格式化
![](/data/attachment/album/201801/15/135408yjwoxwogs0zsgjsg.jpg)


有时你会发现需要格式化某个文本文件中的内容。比如，该文本文件每行一个单词，而任务是把所有的单词都放在同一行。当然，你可以手工来做，但没人喜欢手工做这么耗时的工作。而且，这只是一个例子 - 事实上的任务可能千奇百怪。


好在，有一个命令可以满足至少一部分的文本格式化的需求。这个工具就是 `fmt`。本教程将会讨论 `fmt` 的基本用法以及它提供的一些主要功能。文中所有的命令和指令都在 Ubuntu 16.04LTS 下经过了测试。


### Linux fmt 命令


`fmt` 命令是一个简单的文本格式化工具，任何人都能在命令行下运行它。它的基本语法为：



```
fmt [-WIDTH] [OPTION]... [FILE]...

```

它的 man 页是这么说的：



> 
> 重新格式化文件中的每一个段落，将结果写到标准输出。选项 `-WIDTH` 是 `--width=DIGITS` 形式的缩写。
> 
> 
> 


下面这些问答方式的例子应该能让你对 `fmt` 的用法有很好的了解。


### Q1、如何使用 fmt 来将文本内容格式成同一行？


使用 `fmt` 命令的基本形式（省略任何选项）就能做到这一点。你只需要将文件名作为参数传递给它。



```
fmt [file-name]

```

下面截屏是命令的执行结果：


[![format contents of file in single line](/data/attachment/album/201801/15/135418nswjgm4yd9mmw44d.png)](https://www.howtoforge.com/images/linux_fmt_command/big/fmt-basic-usage.png)


你可以看到文件中多行内容都被格式化成同一行了。请注意，这并不会修改原文件（file1）。


### Q2、如何修改最大行宽？


默认情况下，`fmt` 命令产生的输出中的最大行宽为 75。然而，如果你想的话，可以用 `-w` 选项进行修改，它接受一个表示新行宽的数字作为参数值。



```
fmt -w [n] [file-name]

```

下面这个例子把行宽削减到了 20：


[![change maximum line width](/data/attachment/album/201801/15/135421ms3gnzz81cap3dlj.png)](https://www.howtoforge.com/images/linux_fmt_command/big/fmt-w-option.png)


### Q3、如何让 fmt 突出显示第一行？


这是通过让第一行的缩进与众不同来实现的，你可以使用 `-t` 选项来实现。



```
fmt -t [file-name]

```

[![make fmt highlight the first line](/data/attachment/album/201801/15/135425ds0ormrmo7sllptr.png)](https://www.howtoforge.com/images/linux_fmt_command/big/fmt-t-option.png)


### Q4、如何使用 fmt 拆分长行？


fmt 命令也能用来对长行进行拆分，你可以使用 `-s` 选项来应用该功能。



```
fmt -s [file-name]

```

下面是一个例子：


[![make fmt split long lines](/data/attachment/album/201801/15/135428x9j6sdhu9s6hzhs7.png)](https://www.howtoforge.com/images/linux_fmt_command/big/fmt-s-option.png)


### Q5、如何在单词与单词之间，句子之间用空格分开？


fmt 命令提供了一个 `-u` 选项，这会在单词与单词之间用单个空格分开，句子之间用两个空格分开。你可以这样用：



```
fmt -u [file-name]

```

注意，在我们的案例中，这个功能是默认开启的。


### 总结


没错，`fmt` 提供的功能不多，但不代表它的应用就不广泛。因为你永远不知道什么时候会用到它。在本教程中，我们已经讲解了 `fmt` 提供的主要选项。若想了解更多细节，请查看该工具的 [man 页](https://linux.die.net/man/1/fmt)。




---


via: <https://www.howtoforge.com/linux-fmt-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux fmt Command - Usage and Examples

The **fmt** command is a text formatting tool that simplifies making text more readable by adjusting its layout. It takes input from either a file or standard input and formats the text by filling and joining lines to produce output within a specified width. By default, *fmt* wraps lines to 75 characters, which can be adjusted using command-line options. It's particularly useful for reformatting text documents, email drafts, or any plain text file that needs uniform line lengths, ensuring a cleaner and more consistent content presentation.

In this tutorial, we will discuss the basics of fmt and some of its main features. All commands and instructions mentioned here have been tested on Ubuntu 24.04.

## Linux fmt command

The fmt command is a simple text formatting tool available to users of the Linux command line. Following is its basic syntax:

fmt [-WIDTH] [OPTION]... [FILE]...

And here's how the man page describes it:

Reformat each paragraph in the FILE(s), writing to standard output. The option -WIDTH is an abbreviated form of --width=DIGITS.

The following are some Q&A-style examples that should give you a good idea of fmt's usage.

## Q1. How to format contents of file in single line using fmt?

When used in its basic form (without options), the fmt command does this. You only need to pass the filename as an argument.

fmt [file-name]

The following screenshot shows the command in action:

So you can see that multiple lines in the file were formatted in a way that everything got clubbed up in a single line. Please note that the original file (file1 in this case) remains unaffected.

## Q2. How to change the maximum line width?

By default, the maximum width of a line that *fmt* command produces in output is 75. However, if you want, you can change that using the **-w** command line option, which requires a numerical value representing the new limit.

fmt -w [n] [file-name]

Here's an example where width was reduced to 20:

## Q3. How to make fmt highlight the first line?

This can be done by making the indentation of the first line different from the rest, which you can do by using the **-t** command line option.

fmt -t [file-name]

## Q4. How to make fmt split long lines?

The fmt command can also split long lines, a feature that you can access using the **-s** command line option.

fmt -s [file-name]

Here's an example of this option:

## Q5. How to have separate spacing for words and lines?

The fmt command offers a **-u** option, which ensures one space between words and two between sentences. Here's how you can use it:

fmt -u [file-name]

Note that this feature was enabled by default in our case.

## Conclusion

Agreed, fmt offers limited features, but you can't say it has a limited audience. You never know when you may need it. In this tutorial, we've covered the majority of the command line options that fmt offers. For more details, head to the tool's [man page](https://linux.die.net/man/1/fmt).