Vim 中的智能换行 [英] Smart Wrap in Vim

查看:24
本文介绍了Vim 中的智能换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been wondering if Vim has the capability to smart wrap lines of code, so that it keeps the same indentation as the line that it is indenting. I have noticed it on some other text editor, such as e-text editor, and found that it helped me to comprehend what I'm looking at easier.

例如而不是

<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>

推荐答案

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

This feature has been implemented on June 25, 2014 as patch 7.4.338. There followed a few patches refining the feature, last one being 7.4.354, so that's the version you'll want.

:help breakindent
:help breakindentopt

摘自以下 vim 帮助:

Excerpts from vim help below:

'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 设置,这将使用您指定的字符作为后缀.

Also relevant to this is the showbreak setting, this will suffix your shift amount with character(s) you specify.

" 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.

Note on behaviour

If you don't specify the sbr option, any showbreak any characters put appended to the indentation. Removing sbr from the above example causes an effective indent of 4 characters; with that setting, if you just want to use showbreak without additional indentation, specify shift:0.

您还可以提供负移,这会产生将 showbreak 字符和换行文本拖回任何可用缩进空间的效果.

You can also give a negative shift, which would have the effect of dragging showbreak characters, and wrapped text, back into any available indent space.

当指定一个 min 值时,如果终端宽度变窄,移位的数量将被压缩,但 showbreak 字符总是被保留.

When specifying a min value, the shifted amount will be squashed if you terminal width is narrower, but showbreak characters are always preserved.

这篇关于Vim 中的智能换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆