假设我在VIM编辑器中有超长行(比如大约300多个字符).如何将其分成多行,使单词边界大致突破80个字符?
例:
This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line
Run Code Online (Sandbox Code Playgroud)
至
This is a really long line
This is a really long line
This is a really long line
This is a really long line
This is a really long line
This is a ...
Run Code Online (Sandbox Code Playgroud)
he_*_*eat 243
Vim非常容易这样做(在字边界处断线).
gq{motion} % format the line that {motion} moves over
{Visual}gq % format the visually selected area
gqq % format the current line
...
Run Code Online (Sandbox Code Playgroud)
我建议你检查:help gq和:help gw.
设置textwidth(tw)还会在键入时超出时自动换行.它也被使用gq,但如果禁用gq中断窗口大小或79取决于哪个先出现.
:set tw=80
Run Code Online (Sandbox Code Playgroud)
通过设置格式选项以包括文本宽度,vim将在tw设置时自动中断.
:set fo+=t
Run Code Online (Sandbox Code Playgroud)
小智 82
首先设置你的vim,以便它理解你想要80个字符:
:set tw=80
Run Code Online (Sandbox Code Playgroud)
然后,暮光之城:
V
Run Code Online (Sandbox Code Playgroud)
并使vim重新格式化:
gq
Run Code Online (Sandbox Code Playgroud)
Wer*_*sey 17
这与VIM并不真正相关,但您可以使用fmt程序
$ fmt myfile
Run Code Online (Sandbox Code Playgroud)
smo*_*ice 12
对于实线文本,在正常模式下使用v突出显示区域,然后按
:s/\v(.{80})/\1\r/g
Run Code Online (Sandbox Code Playgroud)
这将在每个第80个字符的末尾添加换行符.
:s/ replaces within the current select
\v uses regular expressions
(.{80}) selects 80 characters & placed them into group one
\1\r replaces group one with group one and a newline
Run Code Online (Sandbox Code Playgroud)
如果你在*nix上,你可能已经fold有了.
选择您想要使用的区域v,然后您可以使用以下方法中断宽度为80的空格:
!fold --spaces --width=80
这与使用完全相同gq.
但是,如果您只想在字符80处中断而不限于空格,则可以使用:
!fold --width=80
如果你只需要一次按键就可以设置一个映射 - 我已经用过了
vmap <f1> !fold --width=80<CR>
要在完整文档中拆分长行而不删除现有的换行符,请使用:
:set formatoptions+=w
:set tw=80
gggqG
Run Code Online (Sandbox Code Playgroud)