Vim中的智能包裹

时间:2009-07-30 02:28:19

标签: vim word-wrap

我一直想知道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>

8 个答案:

答案 0 :(得分:49)

此功能已作为补丁7.4.338 implemented on June 25, 2014。接下来有一些补丁改进了这个功能,最后一个补丁是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.

与此相关的还有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=>>   

关于行为的说明

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

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

指定min值时,如果终端宽度较窄,则移位的金额将被压扁,但始终会保留showbreak个字符。

答案 1 :(得分:33)

有一个补丁,但它已经延续,上次我检查时没有干净利落。请参阅http://groups.google.com/group/vim_dev/web/vim-patches中的“正确缩进包裹线”条目 - 我真的希望这会进入主线。

更新:该链接似乎有点故障。 Here is a more up to date version of the patch

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

答案 2 :(得分:17)

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

:set showbreak=>>>

示例:

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

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

答案 3 :(得分:8)

更新:2014年6月,patch to support a breakindent option合并到Vim(版本7.4.346或更高版本以获得最佳支持)。


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

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

答案 4 :(得分:5)

我知道你能做到这一点的唯一方法是使用返回字符(如Cfreak所述)并将textwidth选项与各种缩进选项结合使用。如果您的缩进配置正确(因为默认情况下我使用的是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.

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

有关详细信息,请参阅:

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

答案 5 :(得分:3)

:set smartindent
:set autoindent

我认为你仍然需要使用返回

答案 6 :(得分:2)

如果您的HTML格式已经很好,那么通过xmllint运行它可能有所帮助:

:%!xmllint --html --format

答案 7 :(得分:2)

宏解决方案:


编辑:

操作gq{motion}自动格式化为“textwidth”变量设置的任何内容。这比使用我的宏80lBi^M更容易/更好。


如果您启用了自动注册

:set autoindent

然后在一行的末尾输入一个返回值将使下一行缩进相同的数量。如果您愿意,可以使用它来硬线输入换行。以下宏利用此功能自动缩进文本:

将寄存器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   

激活宏。几秒钟之后,您的文本将全部显示为80个字符或更少字符的正确缩进行。

说明:

以下是对宏的剖析:

第1部分(宏z):

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部分(宏x):

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,这个宏就会中断。

  • 这个宏不会做一些聪明的事情,比如缩进过去的标记行。

  • 使用lazyredraw设置(:set lazyredraw)来提高速度