How to Wrap Text to a Fixed Width
Why Fixed-Width Text Wrapping Is Needed
In certain scenarios, text must wrap at a fixed width: terminal and command-line output (traditional terminal width is 80 columns); plain text email (RFC 2822 recommends no more than 78 characters per line); code comment blocks (following project line width convention, typically 80 or 120 characters); version control commit messages (convention: first line no more than 72 characters); Markdown document source (maintaining readability).
Soft Wrap vs. Hard Wrap
There is an important distinction between two wrapping methods: Soft Wrap is when an editor automatically folds overly long lines during display, with no actual line breaks in the text โ lines adjust automatically in windows of different widths. Hard Wrap physically inserts line break characters (\n) into the text, splitting long lines at fixed positions. "Wrapping text to a fixed width" refers to inserting hard breaks, not just visual soft wrapping.
Line-Breaking Algorithm Considerations
Wrapping is not as simple as inserting a line break every 80 characters, because that would cut words in the middle. A good wrapping algorithm breaks at the nearest word boundary (word-boundary wrapping), ensuring: no word is cut in the middle; a single long word exceeding the width limit gets its own line; and optionally preserving original paragraph separators (wrapping within paragraphs but keeping blank lines between paragraphs).
Code Implementation
# Python: ไฝฟ็จ textwrap ๆจกๅ
import textwrap
text = "This is a very long sentence that needs to be wrapped at a specific column width."
# ๆ 60 ๅญ็ฌฆๅฎฝๅบฆๆข่ก
wrapped = textwrap.fill(text, width=60)
print(wrapped)
# ๆข่กไฝไฟ็็ผฉ่ฟ
wrapped_indent = textwrap.fill(text, width=60,
initial_indent=' ',
subsequent_indent=' ')
# ๅค็ๆดๆฎตๆๅญ๏ผไฟ็ๆฎต่ฝๅ้๏ผ
paragraphs = text.split('\n\n')
result = '\n\n'.join(textwrap.fill(p, width=72) for p in paragraphs)
Command-Line Tools
# fold ๅฝไปค๏ผๆๅญ็ฌฆๆฐๆ่ก
fold -w 80 input.txt
# fold -s๏ผๅจ็ฉบๆ ผๅคๆ่ก๏ผไธๆชๆญๅ่ฏ๏ผ
fold -s -w 80 input.txt
# fmt ๅฝไปค๏ผๆดๆบ่ฝ็ๆๆฌๆ่ก๏ผๅค็ๆฎต่ฝ๏ผ
fmt -w 72 input.txt
# par ๅทฅๅ
ท๏ผ้ซ็บงๆฎต่ฝๆ ผๅผๅ๏ผๅฆๆๅทฒๅฎ่ฃ
๏ผ
par 72 < input.txt
Best Practices for Code Comments
When writing long comments in code, most style guides recommend limiting comment blocks to 79โ80 characters (including indentation and comment symbols). Python PEP 8 recommends maximum 79 characters for code lines and 72 characters for comments (leaving some margin). Most modern IDEs (VS Code, PyCharm) have built-in line width rulers and can automatically wrap comments. EditorConfig files can unify line width settings for all developers at the project level.
Width Calculation for Chinese Text
Chinese characters (and other full-width CJK characters) occupy two character widths in monospace fonts โ these are called "East Asian wide" characters. When calculating line width, each Chinese character should count as 2 width units, not 1. Python's wcwidth library and the unicodedata.east_asian_width() function help correctly calculate line widths for text containing Chinese characters, ensuring correct wrap positions.
Try the free tool now
Use Free Tool โ