88

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

8 回答 8

67

此功能已于2014 年 6 月 25 日作为补丁 7.4.338 实施。随后有几个补丁完善了该功能,最后一个是 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始终保留字符。

于 2014-09-24T11:30:41.283 回答
33

有一个补丁可以解决这个问题,但它已经存在多年了,上次我检查并没有完全应用。请参阅http://groups.google.com/group/vim_dev/web/vim-patches中的“正确缩进换行”条目——我真的希望这会进入主线。

更新:该链接似乎已经烂掉了。这是补丁的更新版本

更新 2:它已在上游合并(从 7.4.345 开始),所以现在您只需:set breakindent.

于 2010-02-07T17:48:28.100 回答
17

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

:set showbreak=>>>

例子:

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

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

于 2009-07-30T02:39:56.437 回答
9

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


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

与您要查找的内容相近的其他选项是linebreakshowbreak。使用showbreak,您可以修改被换行的行的左边距显示的内容,但不幸的是,它不允许根据当前上下文进行变量缩进。

于 2009-07-30T02:36:10.963 回答
5

我知道您可以这样做的唯一方法是使用返回字符(如 Cfreak 所述)并将该textwidth选项与各种缩进选项结合起来。如果您的缩进配置正确(因为它默认使用我相信的 html 语法,但否则请参阅autoindentandsmartindent选项),您可以:

: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'
于 2009-07-30T07:20:21.160 回答
3
:set smartindent
:set autoindent

我认为你仍然必须使用回报

于 2009-07-30T02:35:04.317 回答
2

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

:%!xmllint --html --format
于 2009-07-30T02:32:28.407 回答
2

宏观解决方案:


编辑:

gq{motion}无论变量“textwidth”设置为什么,操作都会自动格式化。这比使用80lBi^M我的宏更容易/更好。


如果您启用了自动缩进

:set autoindent

然后在一行的末尾输入一个 return 将使下一行缩进相同的数量。如果您愿意,可以使用它来硬输入换行符。以下宏利用这一点自动缩进您的文本:

将寄存器 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) 加快速度

于 2014-02-25T00:17:44.620 回答