Vim:换行文本(“gq”)而不修改带下划线的标题
有没有一种方法可以在 Vim 中格式化文本并尊重带下划线的标题?
在 Markdown 中,有两种表示标题的方式:
#Level 1 heading
##Level 2 heading
###Level 3 heading
1 级和 1 级标题。仅2:
Level 1 heading
===============
Level 2 heading
---------------
我喜欢下划线的风格,因为我认为这样读起来更好。
当我在 Vim 中使用 :set textwidth=72
编写 markdown 时,我希望能够使用 gggqG
重新格式化整个文档,但它会处理这些带下划线的标题作为段落,并将它们挤到一行上。因此,如果我从以下内容开始:
Lorem ipsum
===========
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
在对整个段落运行 gq
后,我最终会得到这样的结果:
Lorem ipsum ===========
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
有什么方法可以阻止 Vim 格式化带下划线的标题吗?
我想必须有一个使用 formatexpr
或 formatprg
。我研究了 par 的文档,尽管非常强大,但看起来这不是其中之一它的特点。所以我想知道是否有另一个外部程序可以与理解 markdown 的 formatprg
一起使用,或者是否可以使用带有 formatexpr
设置的 vimscript 来实现这一点。
Is there a way of formatting text in Vim that respects underlined headings?
In Markdown, there are two ways of representing headings:
#Level 1 heading
##Level 2 heading
###Level 3 heading
and for level 1 & 2 only:
Level 1 heading
===============
Level 2 heading
---------------
I am fond of the underlining style, as I think it reads better.
When I compose markdown in Vim with, say, :set textwidth=72
, I would like to be able to reformat the entire document with gggqG
, but it treats these underlined headings as paragraphs, and squeezes them together onto one line. So if I started with the following:
Lorem ipsum
===========
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
After running gq
on the entire passage, I would end up with something like this:
Lorem ipsum ===========
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.
Is there any way that I can prevent Vim from formatting the underlined headings?
I suppose there must be a solution using either formatexpr
or formatprg
. I have studied the documentation for par, and despite being very powerful it looks as though this is not one of its features. So I'm wondering if there is another external program which could be used with formatprg
that understands markdown, or if this can be achieved instead using vimscript with the formatexpr
setting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种有效的选择是将下划线字符串添加到comments 变量中。
如果您的下划线字符串是固定大小,则可以仅添加以下内容:
如果它们是可变大小(多个):
使用多个允许段落以单个
-
或=
并防止后续行前面添加注释字符串。删除上面的
+
即可仅对这些字符串设置注释,而不是添加它们。在某些情况下,格式会出现意外的情况(例如,连续行上有下划线)。我确信有一种更合适的方法可以做到这一点,但希望这能让您开始。
One option that sorta works is to add the underline strings to the
comments
variable.If your underline strings are a fixed size, you could add just those:
If they're variable size (more than one):
Using more-than-one allows a paragraph to start with a single
-
or=
and keeps subsequent lines from being prepended with the comment string.Remove the
+
above to set comments just to those strings instead of adding them on.There are some cases where the formatting will act unexpectedly (e.g. underlines on consecutive lines). I'm sure there's a more appropriate way to do this but hopefully this will get you started.
至少你可以为它设置一些宏。
例如,将文本以某种方式放置在第一段上,搜索第一个标题下划线,然后向下移动 2 行,然后目视选择下一个下划线减去 3 行的区域,然后对其进行格式化:
现在您可以使用
a< /代码> 宏。
华泰
At least you can set up some macro for it.
E.g. postion the text somehow on the first paragraph with searching for the first headline-underline then move down 2 lines, then visuallí select the area to the next underline minus 3 line, then format it:
Now you can use your
a
macro.HTH