Vim中的智能包裹

Sas*_*sha 75 vim word-wrap

我一直想知道Vim是否具有智能包装代码行的能力,因此它保持与缩进的行相同的缩进.我在其他一些文本编辑器上注意到了它,比如电子文本编辑器,发现它帮助我更容易理解我正在看的内容.

例如而不是

<p>
    <a href="http://www.example.com">
        This is a bogus link, used to demonstrate
an example
    </a>
</p>
Run Code Online (Sandbox Code Playgroud)

它会显示为

<p>
    <a href="somelink">
        This is a bogus link, used to demonstrate
        an example
    </a>
</p>
Run Code Online (Sandbox Code Playgroud)

Dom*_*kis 50

此功能已于2014年6月25日作为补丁7.4.338实施.随后有一些补丁改进了这个功能,最后一个是7.4.354,所以这就是你想要的版本.

:help breakindent
:help breakindentopt
Run Code Online (Sandbox Code Playgroud)

摘自下面的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.
Run Code Online (Sandbox Code Playgroud)

与此相关的还有showbreak设置,这将使您指定的字符后移金额后缀.

配置示例

" enable indentation
set breakindent

" ident by an additional 2 characters on wrapped lines, when line >= 40 characters, put 'showbreak' at start of line
set breakindentopt=shift:2,min:40,sbr

" append '>>' to indent
set showbreak=>>   
Run Code Online (Sandbox Code Playgroud)

关于行为的说明

如果未指定该sbr选项,则会showbreak在缩进中附加任何任何字符.sbr从上面的示例中删除会导致4个字符的有效缩进; 使用该设置,如果您只想在showbreak没有其他缩进的情况下使用,请指定shift:0.

您还可以给出负移位,这会将showbreak字符和包装文本拖回任何可用的缩进空间.

指定min值时,如果终端宽度较窄,则移位的数量将被压扁,但showbreak始终保留字符.

  • 简而言之,将 `set breakindent` 添加到您的 vim 配置将使换行符并在缩进处换行 - 而不是断开并从下一行开始,就好像它是一个新行一样。 (2认同)
  • 并且将"set breakindentopt = shift:4"添加到你的vim配置将缩进额外的4个空格. (2认同)

erg*_*sys 33

有一个补丁,但它已经挥之不去多年,上次我检查不干净.请参阅http://groups.google.com/group/vim_dev/web/vim-patches中的"正确缩进包装线"条目- 我真的希望这会进入主线.

更新:该链接似乎有点扭曲.这是补丁的最新版本.

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

  • 感谢@Vitor Eiji的更新.这是一个很棒的消息. (2认同)

too*_*php 17

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

:set showbreak=>>>
Run Code Online (Sandbox Code Playgroud)

例:

<p>
    <a href="http://www.example.com">
        This is a bogus link, used to demonstrate
>>>an example
    </a>
</p>
Run Code Online (Sandbox Code Playgroud)

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

  • @Jeromy Anglim一个小修复:`set showbreak =\\\\\\\\\\\\\\``比'let&showbreak = repeat('',14)更不易读 (7认同)
  • 你也可以使用`:set showbreak =\\\\\\\\\\\\\\`(反斜杠空格组合使得中断字符空间.因此,你可以添加足够的空格比你的大多数包裹更深代码(例如,10个空格将比2个4个空格标签深,14个比3个空格标签更深);空间的一个很好的特征是它在视觉上不那么分散注意力(如果这就是你想要的那样). (5认同)

Gre*_*ill 8

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


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

接近您正在寻找的其他选项是linebreakshowbreak.使用showbreak,您可以修改包装的行的左边距显示的内容,但不幸的是,根据当前上下文,它不允许变量缩进.


DrA*_*rAl 5

我知道你能做到这一点的唯一方法是使用返回字符(如Cfreak所述)并将textwidth选项与各种缩进选项结合使用.如果您的缩进配置正确(因为它默认情况下使用我认为的html语法,但另外看到autoindentsmartindent选项),您可以:

:set formatoptions = tcqw
:set textwidth = 50
gggqG
Run Code Online (Sandbox Code Playgroud)

如果您对formatoptions设置进行了任何自定义,则最好只执行以下操作:

:set fo += w
:set tw = 50
gggqG
Run Code Online (Sandbox Code Playgroud)

这是做什么的:

: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.
Run Code Online (Sandbox Code Playgroud)

请注意,这与软包装不同:它会将行包装在源文件中以及屏幕上(除非您当然不保存它!).可以添加的其他设置formatoptions将在您键入时自动格式化:详细信息:help fo-table.

有关更多信息,请参阅:

:help 'formatoptions'
:help fo-table
:help 'textwidth'
:help gq
:help gg
:help G
:help 'autoindent'
:help 'smartindent'
Run Code Online (Sandbox Code Playgroud)