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

发布于 2024-08-02 20:37:54 字数 621 浏览 5 评论 0原文

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

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

我可以使用 Editplus 来完成此操作,它是一个内置函数并且运行良好。我之所以想在 Vim 中做这件事,首先 Editplus 只能在 PC 上使用,其次 Vim 太糟糕了。

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

:设置 tw=43
咕咕咕

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

例如

我有这样的文字:

557
00:47:39,487 --> 00:47:42,453
我必须完成一些程序,
我请你看看它们对我来说有什么用

格式化后,我得到:

557 00:47:39,487 --> 00:47:42,453 我会
必须完成一些手续,而我
请您查看它们的用途

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

如何让它不忽略换行符?

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

I want to format srt subtitle text files to avoid wrapping problems on my media player.

I need to set a line wrap width to a number of characters e.g. 43

I can do this with Editplus, its a built in function and works well. The reason I want to do it in Vim, firstly Editplus is only available on the PC and the secondly Vim is badass.

I have found the following solution on the net..

:set tw=43
gggqG

It does work, but not exactly how I want it.

E.g.

I have this text:

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

after I format it, I get:

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

It seems to ignore line breaks/CRs. As you can see the "I will" has been added to the first line.

How do I get it to not ignore line breaks?

EDIT: apoligies about the formatting, first time using stackoverflow!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情话难免假 2024-08-09 20:37:54

您可以使用 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 行将持续整个会话。如果您希望这些选项仅在当前缓冲区上使用,请改用 :setlocal

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

You could use the whitespace option of formatoptions and make the lines you want to wrap end in whitespace.

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

The third line adds a space on the end of any line starting with a letter and fo+=w stops gq from joining lines that don't end in spaces.

See:

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

Edit in response to comments

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

This translates to:

: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)

The global (:g) command will only operate on the current file, but the textwidth and formatoptions lines will last for the whole session. If you want those options to only be used on the current buffer, use :setlocal instead:

:setlocal tw=43
:setlocal fo+=w
:help :setlocal
话少心凉 2024-08-09 20:37:54

如果您想要换行的唯一行是以文本开头的行(忽略以数字开头的行),您可以使用以下内容。

:g/^[a-zA-Z]/normal gqq

这将运行 p00ya 的命令文件中的每个文本行。

If the only lines you want to wrap are the ones that start with text (ignore the ones that start with numbers) you can use the following.

:g/^[a-zA-Z]/normal gqq

This will run p00ya's command on each text line in the file.

忆沫 2024-08-09 20:37:54

包含以下文本(即只有3行):

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

设置tw后,在第3行使用gqq(格式化当前行)。按段落格式化将不起作用,因为 vim 会将所有 3 行视为同一段落的一部分(段落仅以空行结尾)。

With the following text (i.e. only 3 lines):

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

Use gqq (format current line) on the 3rd line after setting tw. Formatting by paragraph will not work since vim will treat all 3 lines as part of the same paragraph (paragraphs only end with a blank line).

半葬歌 2024-08-09 20:37:54

虽然不完全健壮(或优雅),但如果您可以假设字幕行不以数字开头,则可以配置 Vim 将它们视为注释。

:set comments=:0,:1,:2,:3,:4,:5,:6,:7,:8,:9
gggqG

以 0-9 开头的行将被视为注释,不会与文本合并在一起。

While not exactly robust (or elegant), if you can assume that the subtitle lines do not begin with numbers, you can configure Vim to treat them as comments.

:set comments=:0,:1,:2,:3,:4,:5,:6,:7,:8,:9
gggqG

Lines starting with 0-9 will be treated as comments and won't be merged together with the text.

咆哮 2024-08-09 20:37:54

我无法确切地看出问题是由于您的帖子格式造成的,但您可能需要检查 vim 帮助中的“formatoptions”设置,特别是 :he fo-table,其中包含一堆自定义标志。

此链接可能有用。

I can't see exactly what the issue is due to the formatting of your post, but you might want to checkout the 'formatoptions' setting in vim's help, specifically :he fo-table which contains a bunch of customization flags.

This link might be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文