使用 Vim 更改文本文件中的换行宽度
我想格式化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
formatoptions
的空白选项并使要换行的行以空白结束。第三行在任何以字母开头的行末尾添加一个空格,并且
fo+=w
阻止gq
连接不以空格结尾的行。请参阅:
编辑以响应评论
这翻译为:
全局(
:g
)命令将仅对当前文件进行操作,但textwidth
和formatoptions
行将持续整个会话。如果您希望这些选项仅在当前缓冲区上使用,请改用:setlocal
:You could use the whitespace option of
formatoptions
and make the lines you want to wrap end in whitespace.The third line adds a space on the end of any line starting with a letter and
fo+=w
stopsgq
from joining lines that don't end in spaces.See:
Edit in response to comments
This translates to:
The global (
:g
) command will only operate on the current file, but thetextwidth
andformatoptions
lines will last for the whole session. If you want those options to only be used on the current buffer, use:setlocal
instead:如果您想要换行的唯一行是以文本开头的行(忽略以数字开头的行),您可以使用以下内容。
这将运行 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.
This will run p00ya's command on each text line in the file.
包含以下文本(即只有3行):
设置
tw
后,在第3行使用gqq
(格式化当前行)。按段落格式化将不起作用,因为 vim 会将所有 3 行视为同一段落的一部分(段落仅以空行结尾)。With the following text (i.e. only 3 lines):
Use
gqq
(format current line) on the 3rd line after settingtw
. 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).虽然不完全健壮(或优雅),但如果您可以假设字幕行不以数字开头,则可以配置 Vim 将它们视为注释。
以 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.
Lines starting with 0-9 will be treated as comments and won't be merged together with the text.
我无法确切地看出问题是由于您的帖子格式造成的,但您可能需要检查 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.