gpt4 book ai didi

text - 使用 Vim 更改文本文件中的换行宽度

转载 作者:行者123 更新时间:2023-12-01 08:42:02 26 4
gpt4 key购买 nike

我想格式化 srt 字幕文本文件以避免在我的媒体播放器上出现换行问题。

我需要将换行宽度设置为多个字符,例如43

我可以使用 Editplus 来做到这一点,它是一个内置功能并且运行良好。我想在 Vim 中做的原因,首先 Editplus 只能在 PC 上使用,其次 Vim 很糟糕。

我在网上找到了以下解决方案..

:set tw=43
gggqG

它确实有效,但不完全是我想要的。

例如

我有这段文字:

557
00:47:39,487 --> 00:47:42,453
I will have to complete some procedures,
and I asked you to check out what they are for me

格式化后,我得到:

557 00:47:39,487 --> 00:47:42,453 I will
have to complete some procedures, and I
asked you to check out what they are for
me

它似乎忽略了换行符/CR。如您所见,“我会”已添加到第一行。

如何让它不忽略换行符?

编辑:关于格式的apoligies,第一次使用stackoverflow!

最佳答案

您可以使用 formatoptions 的空白选项,并将要换行的行以空白结尾。

:set tw=43
:set fo+=w
:g/^\a/s/$/ /
gggqG

第三行在任何以字母开头的行末尾添加一个空格,fo+=w 阻止 gq 加入不以空格结尾的行。

见:

:help fo-table
:help 'formatoptions'
:help gq
:help :g

根据评论进行编辑

:g/^\a/s/$/ /

这转化为:

:g/   " Search the file
^\a " For lines starting (^) with an alphabetic character (\a - equivalent to [A-Za-z])
/ " Then on each line that the regexp matches (i.e. each line starting with an alphabetic character)
s/ " Substitute...
$ " The end of line (zero-width match at the end of the line)
/ / " With a space (slashes are delimiters)

全局 (:g) 命令只会对当前文件进行操作,但 textwidthformatoptions 行将持续整个 session 。如果您希望这些选项仅用于当前缓冲区,请改用 :setlocal:

:setlocal tw=43
:setlocal fo+=w
:help :setlocal

关于text - 使用 Vim 更改文本文件中的换行宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1318470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com