4

除了普通文本之外,我不经常重新格式化文本,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. 

即保留原始行结尾,但将比 textwidth 长的每一行“分解”为几行。因此它符合预定义的列宽限制。

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

4

5 回答 5

5

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

:norm gqq

gqq 重新格式化一行。:norm with a range 对范围内的每个单独应用一个普通代码。这意味着您gqq分别在每一行上应用。并且因为您textwidth的设置为一定的长度(例如80),这意味着较短的行将不会被连接/换行。

我已经在您的示例文本上对此进行了测试,它只是给出了您想要的。

顺便说一句,您可以使用 vim 使用 :formatprg外部 prg 对其进行修改。这可以更好地控制您想要使用外部应用程序修改的内容。欲了解更多信息,请阅读 :h formatprg

于 2012-06-08T19:08:49.510 回答
2

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

:set wrap
:set linebreak
于 2012-06-08T18:27:26.640 回答
0

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

于 2012-06-08T18:21:39.640 回答
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.
于 2012-06-08T18:28:44.473 回答
0

如果除最后一行之外的系列的每一行都以空格结尾,您可以gq认为一系列行属于一个段落:

set formatoptions+=w

. 在此设置之后gq将不会在您的示例中加入行(除非您有尾随空格)并且您仍然可以使用:%s/ \n/ /. 另一种方法是在每个当前行之间添加空行。

我也建议做

set list listchars+=trail:-

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

于 2012-06-09T04:21:46.463 回答