在Vim的智能自动换行

问题描述 投票:73回答:8

我一直在想,如果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>
vim word-wrap
8个回答
50
投票

此功能已被implemented on June 25, 2014补丁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字符总是保留。


33
投票

没有此补丁,但它一直萦绕多年,我最后一次检查没有干净的应用。见http://groups.google.com/group/vim_dev/web/vim-patches的“正确缩进换行”项 - 我真的希望这会得到在主线。

更新:该链接似乎已经bitrotted。 Here is a more up to date version of the patch

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


17
投票

我不认为这是可能的具有完全相同的缩进,但你仍然可以得到通过设置选项“showbreak”更好的视野。

:set showbreak=>>>

例:

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

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


8
投票

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


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

接近你在找什么其他选项是linebreakshowbreak。随着showbreak,您可以修改在被换行的左边空白处显示,但不幸的是它不允许变量缩进根据当前的环境。


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'

3
投票
:set smartindent
:set autoindent

我想你还是要使用,虽然回报


2
投票

如果您的HTML已充分形成,通过xmllint运行可能会有所帮助:

:%!xmllint --html --format

2
投票

A Macro Solution:


Edit:

该操作gq{motion}自动格式到任何变量“文本宽度”设置为。这比使用80lBi^M我有我的宏更容易/更好。


如果你已经自动缩进启用

:set autoindent

然后进入在一行的末尾的返回将缩进下一行的相同量。您可以用它来硬的linewraps输入,如果你愿意的话。下面的宏利用这一点来自动缩进您的文本:

设置寄存器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个字符或更少的正确缩进线。

Explanation:

下面是宏的解剖:

第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个字符或更长此宏将打破。
  • 这个宏不会做聪明事一样缩进线过去的标签。
  • 使用lazyredraw设置(:设置lazyredraw)加快这
© www.soinside.com 2019 - 2024. All rights reserved.