Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
智能包裹Vim_Vim_Word Wrap - Fatal编程技术网

智能包裹Vim

智能包裹Vim,vim,word-wrap,Vim,Word Wrap,我一直在想Vim是否有能力智能包装代码行,以便它保持与缩进行相同的缩进。我在其他一些文本编辑器(如电子文本编辑器)上注意到了这一点,并发现它帮助我更容易理解我正在查看的内容 例如,而不是 <p> <a href="http://www.example.com"> This is a bogus link, used to demonstrate an example </a> </p> 看起来像 <p&

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


如果您的HTML格式足够好,那么运行它可能会有帮助:

:%!xmllint --html --format
我认为您仍然必须使用return,尽管更新了:2014年6月,将a合并到Vim中(版本7.4.346或更高版本以获得最佳支持)


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


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

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

:set showbreak=>>>
例如:

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


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

据我所知,唯一可以做到这一点的方法是使用返回字符(如Cfreak所述),并将
textwidth
选项与各种缩进选项相结合。如果缩进配置正确(我认为默认情况下是html语法,但其他情况下请参见
autoindent
smartindent
选项),则可以:

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

这有一个补丁,但它已经徘徊多年,上次我检查并没有适用于干净。请参阅中的“正确缩进包装行”条目——我真的希望这会出现在主线中

更新:该链接似乎已被破坏

更新2:它已处于上游(从7.4.345开始),因此现在您只需
:设置breakindent

宏解决方案:
编辑: operate
gq{motion}
auto将变量“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):

第2部分(宏x):

注意事项:
  • 如果存在80个字符或更长的单词,此宏将中断

  • 此宏将不会执行智能操作,如缩进标记线

  • 使用lazyredraw设置(:set lazyredraw)可加快此速度

此功能已作为补丁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
设置,它将用指定的字符作为移位量的后缀

示例配置 关于行为的说明 如果未指定
sbr
选项,则任何
showbreak
字符都会附加到缩进中。从上述示例中删除
sbr
,将导致4个字符的有效缩进;在该设置下,如果您只想使用
showbreak
而不使用其他缩进,请指定
shift:0

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


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

您也可以使用
:set showbreak=\\\\\\\\\\\\\\\\\\\\\
(反斜杠和空格的组合构成了分隔符空格。因此,您可以添加足够的空格,使其比大多数包装代码更深(例如,10个空格比2个4个空格制表符深,14个空格比3个4个空格制表符深);该空格的一个很好的特性是,它在视觉上不太分散注意力(如果您想要的话)@Jeromy Anglim有一个小的修正:
set showbreak=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
我非常喜欢,我会让html标记更易于阅读。我在该网站上没有看到任何补丁。其他人能确认吗?显然,该网站最近重建了,这些链接都断了。在同一网站上,最近有一篇博客文章甚至有一个更新的补丁。这是真的丢失了。我不明白为什么这不在主线中,尤其是since该修补程序编写得非常好。感谢@Vitor Eiji的更新。这是一个非常好的消息。另请参阅,当情况发生变化时,您应该重新检查答案。使用
:set autoindent
:set smartindent
简言之,将
set breakindent
添加到vim配置中会使行在缩进处中断和换行,而不是br将下一行作为新行开始说话。并将
set breakindentopt=shift:4
添加到vim配置中,将额外缩进4个空格。添加
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
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)
:help breakindent
:help breakindentopt
'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.
" 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=>>