国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > fmt 以获得更好的体验。

# Bash 的 fmt 命令

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

!subtitle:功能

简单文本格式化。

!subtitle:类型

可执行文件(/usr/bin/fmt),属于 coreutils

!subtitle:参数

  • WIDTH - 每行宽度;同 -w 选项,默认为 75

  • OPTION 选项:

    • -c, --crown-margin - 保留前两行的缩进

    • -p, --prefix=STRING - 仅格式化以 STRING 开头的行,并将前缀重新附加到格式化的行

    • -s, --split-only - 拆分长行,但不补充

    • -t, --tagged-paragraph - 第一行的缩进与第二行不同

    • -u, --uniform-spacing - 单词之间空一格,句子之后空两格

    • -w, --width=WIDTH - 每行宽度;默认为 75

    • -g, --goal=WIDTH - 目标宽度(单词提前换行);默认为行宽的 93%

    • --help - 显示帮助

    • --version - 显示版本

  • FILE 文件列表;如果没有这个参数或指定为 -,则读取标准输入

# 示例

$ echo 'Primers 编程伙伴 https://xplanc.org/primers/' > 1.txt   # 创建文件
$ cat 1.txt                                                     # 查看文件
Primers 编程伙伴 https://xplanc.org/primers/
$ fmt -w 40 1.txt                                               # 行宽度 40
Primers 编程伙伴
https://xplanc.org/primers/
$ fmt -w 40 -g 10 1.txt                                         # 行宽度 40 目标宽度 10
Primers
编程伙伴
https://xplanc.org/primers/

# 推荐阅读

# 手册

FMT(1)                           User Commands                          FMT(1)

NAME
       fmt - simple optimal text formatter

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

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

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       -c, --crown-margin
              preserve indentation of first two lines

       -p, --prefix=STRING
              reformat  only lines beginning with STRING, reattaching the pre‐
              fix to reformatted lines

       -s, --split-only
              split long lines, but do not refill

       -t, --tagged-paragraph
              indentation of first line different from second

       -u, --uniform-spacing
              one space between words, two after sentences

       -w, --width=WIDTH
              maximum line width (default of 75 columns)

       -g, --goal=WIDTH
              goal width (default of 93% of width)

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Ross Paterson.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/fmt>
       or available locally via: info '(coreutils) fmt invocation'

GNU coreutils 9.4                 April 2024                            FMT(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14