在Vim中重新格式化,这是明智之举

时间:2012-06-08 17:59:12

标签: vim

除了简单的gq之外,我不经常重新格式化文本,所以这可能很简单,但似乎没有在帮助中找到它的运气。

无论如何,我的文字看起来像这样

Funnily enough, that was exciting. 
"I've just about had enough of this," said a voice beside him. 
He looked up. A girl had come down the other path. Her face was red with exertion under the pale make-up, her hair hung over her eyes in ridiculous ringlets, and she wore a dress which, while clearly made for her size, was designed for someone who was ten years younger and keen on lace edging. 
She was quite attractive, although this fact was not immediately apparent. 
"And you know what they say when you complain?" she demanded. This was not really addressed to Victor. He was just a convenient pair of ears. 

在Vim读到这很痛苦。所以我尝试用gq重新格式化它,这给了我这个

Funnily enough, that was exciting.  "I've just about had enough of this,"
said a voice beside him.  He looked up. A girl had come down the other path.
Her face was red with exertion under the pale make-up, her hair hung over
her eyes in ridiculous ringlets, and she wore a dress which, while clearly
made for her size, was designed for someone who was ten years younger and
keen on lace edging.  She was quite attractive, although this fact was not
immediately apparent.  "And you know what they say when you complain?" she
demanded. This was not really addressed to Victor. He was just a convenient
pair of ears. 

这是无用的,因为在这种情况下原始行结尾具有特殊含义。我想要完成的是这个

Funnily enough, that was exciting. 
"I've just about had enough of this," said a voice beside him. 
He looked up. A girl had come down the other path. Her face was red with
exertion under the pale make-up, her hair hung over her eyes in ridiculous
ringlets, and she wore a dress which, while clearly made for her size, was
designed for someone who was ten years younger and keen on lace edging. 
She was quite attractive, although this fact was not immediately apparent. 
"And you know what they say when you complain?" she demanded. This was not
really addressed to Victor. He was just a convenient pair of ears. 

即。保持原始行结尾,但要将每行比文本宽度“断开”成几行。因此它符合预定义的列宽限制。

任何人对如何做到这一点都有任何想法?这是一个相当大的文档,我需要一些方法来处理它。

5 个答案:

答案 0 :(得分:5)

直观地选择所有行,然后在ex模式下执行:

:norm gqq

gqq重新格式化一行。 :带范围的范数在范围内单独应用普通代码。这意味着您可以分别在每一行上应用gqq。并且因为您的textwidth设置为特定长度(例如80),这意味着较短的行不会被连接/包裹。

我已经在你的示例文本上对此进行了测试,它只是提供了你想要的内容。

顺便说一下,您可以使用vim的:formatprg来修改外部prg。这样可以更好地控制您希望使用外部应用程序修改的内容。有关详细信息,请阅读:h formatprg

答案 1 :(得分:2)

您是否只是想以阅读为目的?如果是这样,你应该考虑在分词时打开换行。在命令模式下:

:set wrap
:set linebreak

答案 2 :(得分:0)

假设这是在Linux上,有许多实用程序可以执行您想要的操作 - fmtroff/nroff/troff和变体等。fmt是我经常使用的工具,但是要求每个段落之间都有一个空行 - 尽管如此,这很容易在vim中完成。因此,您可以添加空行,保存文件,然后按fmt -76运行,例如将每行限制为76个字符。

答案 3 :(得分:0)

一种原始的方式,但通常设法用

来做
tw=80
qa (recording a macro)
  Vgq
q (stop recording)

nmap <C-p> :execute "normal! @a"<cr>

and by holding <C-p> for quite a while. Not the most elegant of solutions but worked.

答案 4 :(得分:0)

如果系列的每一行除了最后一行以空格结尾,你可以让gq认为一系列行属于一个段落:

set formatoptions+=w

。在此设置之后,gq将不会在示例中加入行(除非您在那里有尾随空格),您仍然可以使用:%s/ \n/ /将它们连接起来。另一种方法是在每个当前行之间添加空行。

我也建议做

set list listchars+=trail:-

为了不让vim看到段落结束的位置,而是能够自己看到这个(这个设置会显示尾随的空格)。