有没有工具能够让纯文本段落超过一定字母数量自动换行?

2022-12-31 11:53:17 +08:00
 UnknoownUser

比如我有一段文字

This document specifies an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements.

我想要固定文本每行的字母数,超过了自动换行,如下:

This document specifies an Internet standards track 
protocol for the Internet community, and requests 
discussion and suggestions for improvements.

有什么工具能够达到这一效果?( P.S.我了解到 vscode 的 word wrap 可以自动换行,但是它是在屏幕上显示换行,并不是在一行的末尾真正地插入了一个换行符。)

1433 次点击
所在节点    问与答
9 条回复
UnknoownUser
2022-12-31 11:54:20 +08:00
可能有这样的 vscode 插件,但是我不知道?
aijam
2022-12-31 12:01:04 +08:00
yfugibr
2022-12-31 12:07:07 +08:00
vim,
:set tw=80
选中
gq
xuchkang171
2022-12-31 12:39:21 +08:00
「但是它是在屏幕上显示换行」的功能叫 soft wrap ,不改变文本;
「在一行的末尾真正地插入了一个换行符」则是 hard wrap ,改变文本。

BBEdit 编辑器的 hard wrap 功能入口在 Text > Hard Wrap...
TrembleBeforeMe
2022-12-31 12:49:38 +08:00
fill-column
wenmin92
2022-12-31 13:12:08 +08:00
linux 的 fold 命令。
```
fold (1) - wrap each input line to fit in specified width
```
UnknoownUser
2022-12-31 18:50:15 +08:00
@aijam 完美地解决了我的问题
jones2000
2022-12-31 22:44:26 +08:00
canvas 用 measureText 自己算下, 不就知道一行能显示多少个了, 然后 fillText 一行一行输出不就可以了, 十几行代码的事。
ireqhawk
2022-12-31 22:52:42 +08:00
如果你是为了便于查看,那么 word wrap 功能已经很完美解决这个问题了.
--因为你的窗口可能会放大或者缩小,所以固定字符插入换行并不适用.
如果你的需求真的是要固定字符插入一个换行,这不管什么语言都很好实现吧.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/905754

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX