Vim中的智能包装

我一直在想,Vim是否有能力巧妙地包装代码行,以便它保持与缩进行相同的缩进。 我已经在其他文本编辑器(如电子文本编辑器)上注意到了它,并且发现它帮助我更容易理解我正在查看的内容。

例如,而不是

<p> <a href="http://www.example.com"> This is a bogus link, used to demonstrate an example </a> </p> 

它会显示为

 <p> <a href="somelink"> This is a bogus link, used to demonstrate an example </a> </p> 

该function已于2014年6月25日作为补丁7.4.338实施。 接下来有几个补丁改进了function,最后一个是7.4.354,所以这是你想要的版本。

 :help breakindent :help breakindentopt 

vim的摘录如下:

 'breakindent' 'bri' boolean (default off) local to window {not in Vi} {not available when compiled without the |+linebreak| feature} Every wrapped line will continue visually indented (same amount of space as the beginning of that line), thus preserving horizontal blocks of text. 'breakindentopt' 'briopt' string (default empty) local to window {not in Vi} {not available when compiled without the |+linebreak| feature} Settings for 'breakindent'. It can consist of the following optional items and must be seperated by a comma: min:{n} Minimum text width that will be kept after applying 'breakindent', even if the resulting text should normally be narrower. This prevents text indented almost to the right window border occupying lot of vertical space when broken. shift:{n} After applying 'breakindent', wrapped line beginning will be shift by given number of characters. It permits dynamic French paragraph indentation (negative) or emphasizing the line continuation (positive). sbr Display the 'showbreak' value before applying the additional indent. The default value for min is 20 and shift is 0. 

这有一个补丁,但它一直拖延多年 ,最后一次检查没有干净地应用。 请参阅http://groups.google.com/group/vim_dev/web/vim-patches中的“正确缩进包装行”条目; – 我真的希望这将在主线。

更新:该链接似乎有点滴滴。 这是一个更新版本的补丁 。

更新2:它已经合并到上游(截至7.4.345),所以现在你只需要:set breakindent

我不认为有可能有完全相同的缩进,但您仍然可以通过设置“showbreak”选项获得更好的视图。

 :set showbreak=>>> 

例:

 <p> <a href="http://www.example.com"> This is a bogus link, used to demonstrate >>>an example </a> </p> 

真实的东西看起来比上面的示例代码更好,因为Vim对'>>>'使用了不同的颜色。

更新:2014年6月,一个支持breakindent选项的补丁被合并到Vim中(7.4.346或更高版本获得最佳支持)。


你也可以尝试:set nowrap ,这将允许vim通过向右滚动显示长行。 这对于检查文档的整体结构可能是有用的,但对于实际编辑可能不太方便。

其他select接近你在找什么是linebreakshowbreak 。 使用showbreak ,您可以修改显示在包装行的左边界处的内容,但不幸的是,它不允许根据当前上下文进行variables缩进。

我知道你可以这样做的唯一方法是使用返回字符(如Cfreak所述),并将textwidth选项与各种缩进选项组合在一起。 如果您的缩进configuration正确(因为它是默认的html语法,我相信,但否则看到autoindentsmartindent选项),您可以:

 :set formatoptions = tcqw :set textwidth = 50 gggqG 

如果您有任何formatoptions设置的自定义,最好简单地做:

 :set fo += w :set tw = 50 gggqG 

这是什么:

 :set fo+=w " Add the 'w' flag to the formatoptions so " that reformatting is only done when lines " end in spaces or are too long (so your <p> " isn't moved onto the same line as your <a...). :set tw=50 " Set the textwidth up to wrap at column 50 gg " Go to the start of the file gq{motion} " Reformat the lines that {motion} moves over. G " Motion that goes to the end of the file. 

请注意,这与软包装不同:它将包裹源文件中的行以及屏幕上的行(除非您不保存它当然!)。 还有其他一些设置可以添加到格式选项中,这些设置将在input时自动格式化:详情请参阅:help fo-table

有关更多信息,请参阅:

 :help 'formatoptions' :help fo-table :help 'textwidth' :help gq :help gg :help G :help 'autoindent' :help 'smartindent' 
 :set smartindent :set autoindent 

我想你还是要用回报

如果您的HTML格式完整,通过xmllint运行它可能会有所帮助:

 :%!xmllint --html --format 

macros观解决scheme:


编辑:

操作gq{motion}自动格式化为variables“textwidth”所设置的任何值。 这比使用我用于macros的80lBi^M更容易/更好。


如果你有自动启用

 :set autoindent 

然后在一行的末尾input一个回车,将会使下一行缩进相同的数量。 如果你愿意的话,你可以用这个来拼命inputlinewraps。 下面的macros利用这个来自动缩进你的文本:

将寄存器z设置为:

 gg/\v^.{80,}$^M@x (change 80 to whatever length you want your text to be) 

并将寄存器x设置为:

 80lBi^M^[n@x (change 80 to whatever length you want your text to be) 

然后做

 @x 

激活macros。 几秒钟后,文本将全部以80个字符或更less的正确缩进行。

说明:

这里是一个macros的解剖:

第1部分(macrosz):

 gg/\v^.{80,}$^M@x gg - start at the top of the file (this avoids some formatting issues) / - begin search \v - switch search mode to use a more generic regex input style - no weird vim 'magic' ^.{80,}$ - regex for lines that contain 80 or more characters ^M - enter - do the search (don't type this, you can enter it with ctrl+v then enter) @x - do macro x 

第2部分(macrosx):

 80lBi^M^[n@x 80l - move right 80 characters B - move back one WORD (WORDS include characters like "[];:" etc.) i^M - enter insert mode and then add a return (again don't type this, use ctrl+v) ^[ - escape out of insert mode (enter this with ctrl+v then escape) @x - repeat the macro (macro will run until there are no more lines of 80 characters or more) 

注意事项:

  • 如果有80个字符或更长的WORD,该macros将会中断。

  • 这个macros不会像过去标签的缩进线那样做聪明的事情。

  • 使用lazyredraw设置(:set lazyredraw)加快速度