Linux文本格式化命令fmt

发布于:2025-09-02 ⋅ 阅读:(22) ⋅ 点赞:(0)

fmt 是一个简单但实用的文本格式化命令,它的核心功能是重新填充段落,使文本看起来更整齐,每行的宽度大致相等。

下面通过一系列实战案例来展示它的用法。


一、基础用法:格式化文本文件

这是 fmt 命令最直接的用途。

场景:你有一个文本文件 note.txt,内容杂乱无章,行长短不一,非常不便于阅读。

# 查看原始内容
cat note.txt
This is a long line that just keeps going on and on without any regard for proper line breaks or margins. It's really quite messy.
This is a new paragraph.

Another short line.

使用 fmt 进行格式化

# 基本格式化(默认宽度约75个字符)
fmt note.txt

# 或者将结果输出到新文件
fmt note.txt > note_formatted.txt

格式化后的效果

This is a long line that just keeps going on and on without any regard for
proper line breaks or margins. It's really quite messy.

This is a new paragraph.

Another short line.

效果对比

  • Before: 一段话挤在一行,需要横向滚动才能看完。
  • After: 文本被自动换行,每行宽度均匀,段落之间的空行得以保留,更符合阅读习惯。

二、控制输出宽度 (-w--width)

默认宽度通常是 75,但你可以用 -w 参数指定任何你想要的宽度。

场景:你想将文本格式化为更窄的列,例如用于生成代码注释或在窄终端中显示。

# 格式化为每行最多50个字符
fmt -w 50 note.txt

# 效果:
This is a long line that just keeps going on
and on without any regard for proper line
breaks or margins. It's really quite messy.

This is a new paragraph.

Another short line.

场景:生成宽幅文本。

# 格式化為每行100个字符(如果终端足够宽)
fmt -w 100 note.txt

三、处理标准输入(管道 |

这是 fmt 最强大的用法之一,可以与其他命令组合使用。

场景1:格式化 echo 输出的长句子。

echo "This is a very long sentence that was created with the echo command and needs to be wrapped properly to be readable." | fmt

输出

This is a very long sentence that was created with the echo command and needs
to be wrapped properly to be readable.

场景2:格式化 git commit 消息的草稿。
如果你在命令行写了一个很长的提交信息,可以先格式化再提交。

echo "Fixed a critical bug in the user authentication module that occurred under heavy load when the database connection pool was exhausted." | fmt -w 72
# (72是提交信息一行的推荐宽度)

输出

Fixed a critical bug in the user authentication module that occurred
under heavy load when the database connection pool was exhausted.

四、实战组合技巧

1. 整理源代码中的长注释

场景:代码文件中有一些很长的手写注释,你想让它更整齐。

# 假设有一段Python代码注释很乱
cat << 'EOF' > messy_comment.py
# This function is responsible for handling the request validation and transformation logic. It's a crucial part of our API gateway and must be efficient. We need to check headers, body, and query parameters.
def some_function():
    pass
EOF

# 使用sed等工具提取注释行,用fmt处理,再写回(这是一个简化的例子)
# 更安全的方法是在编辑器中操作,但这展示了可能性
fmt -w 60 messy_comment.py > temp_file && mv temp_file messy_comment.py

整理后

# This function is responsible for handling the request
# validation and transformation logic. It's a crucial part
# of our API gateway and must be efficient. We need to check
# headers, body, and query parameters.
def some_function():
    pass
2. 快速格式化剪贴板内容(macOS)

场景:你从网页上复制了一段没有换行的文本,粘贴到终端后很难读。

# 在macOS上,用 pbpaste 和 pbcopy 与剪贴板交互
pbpaste | fmt | pbcopy

执行后,剪贴板中的内容就被重新格式化好了,你可以直接粘贴到任何地方。

在 Linux 上(使用 xclip)

xclip -selection clipboard -o | fmt | xclip -selection clipboard
3. fold 命令的区别

fmt智能换行,它会尽量保持单词的完整性,并在句号后优先换行。
fold机械换行,它会在精确的字符位置切断,不管是否在单词中间。

对比

echo "hello world this is a test" | fmt -w 10
# 输出:
hello
world this
is a test

echo "hello world this is a test" | fold -w 10
# 输出:
hello worl
d this is
a test

结论:对于格式化自然语言段落,总是使用 fmt


五、常用选项总结

选项 含义 示例
-w, --width=N 设置输出行的最大宽度为 N 个字符 fmt -w 80 file.txt
-s, --split-only 只断行,不合并。防止将短行合并成长行 fmt -s file.txt
-t, --tagged-paragraph 启用“标记段落”模式,首行缩进的处理方式不同 fmt -t file.txt
-u, --uniform 统一空格:在一个单词和两个句子之间留一个空格 fmt -u file.txt

-s (split-only) 场景示例
如果你的文件里有很多短行(比如一个列表),你不想让它们被合并。

cat list.txt
- Item one
- Item two
- Item three

fmt -s list.txt # 会保持原样,不会把三个项目合并成一行

总结

fmt 是一个被低估的 Unix 小工具。它的核心价值在于:

  • 快速整理:让杂乱的文本瞬间变整洁。
  • 脚本自动化:在管道中自动处理文本流,无需打开编辑器。
  • 统一格式:强制文本遵循一定的宽度规范。

下次当你从网上复制大段文字,或者看到脚本里杂乱的长注释时,别忘了在终端里用 fmt 命令拯救一下它的可读性。


网站公告

今日签到

点亮在社区的每一天
去签到