用Vim重新格式化,明智的方法

用Vim重新格式化,明智的方法,vim,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

除了普通的
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. 
i、 e.保留原来的行尾,但将每行长度超过textwidth的内容“打断”为几行。因此,它符合预定义的列宽限制


有人知道怎么做吗?这是一个相当大的ish文档,我需要一种完整的方式来处理它。

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

您只是为了阅读目的而这样做吗?如果是这样的话,你应该考虑在断字时打开线包装。在命令模式下:

:set wrap
:set linebreak

这是一种原始的方式,但总的来说是用

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.
tw=80
qa(录制宏)
Vgq
q(停止录制)
nmap:执行“正常!@a”
并坚持了一段时间。虽然不是最优雅的解决方案,但效果很好。

目视选择所有行,然后在ex模式下执行:

:norm gqq
gqq重新格式化一行:具有范围的norm将正常代码分别应用于范围中的每个in。这意味着您在每一行上分别应用
gqq
。而且由于
textwidth
设置为特定长度(例如
80
),这意味着较短的行不会被合并/包装

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


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

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

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

我也建议这样做

set list listchars+=trail:-

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

它的简写形式,我更新了我的答案,以包括在命令模式下输入的两个命令的长格式。这应该将长线环绕到窗口的宽度,只在“breakat”选项中的字符处断开,在大多数情况下,这不需要自定义。这就是我如何运行Vim来处理发送给web的长文章,因为它保持“虚假”换行符将从要粘贴的文本中分离出来。或者只需执行:%norm gqq即可应用于缓冲区中的所有行。