【问题标题】:Smart Wrap in VimVim 中的智能换行
【发布时间】:2009-07-30 02:28:18
【问题描述】:

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


【解决方案1】:

此功能已作为补丁 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 字符。

【讨论】:

  • 简而言之,将 set breakindent 添加到您的 vim 配置将使换行符在缩进处换行 - 而不是像换行一样从下一行开始换行。
  • 添加set breakindentopt=shift:4到你的vim配置会额外缩进4个空格。
  • breakindentopt 添加更多示例将有助于此答案。帮助文档中的语法并不明显。感谢@fritzo 在您的评论中提供示例。
【解决方案2】:

有一个补丁可以解决这个问题,但它已经存在了 ,而且我上次检查时没有完全应用。请参阅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

【讨论】:

  • 我非常喜欢,我会让 html 标记方式更具可读性
  • 我在那个网站上没有看到任何补丁。其他人可以确认吗?
  • 显然该站点最近已重建,并且这些链接已损坏。在同一个网站上,最近有一篇带有更新补丁的博客文章。
  • 这个真的不见了。我不明白为什么这不在主线中,尤其是。因为补丁写得很好。
  • 感谢@Vitor Eiji 的更新。这是个好消息。
【解决方案3】:

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

:set showbreak=>>>

例子:

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

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

【讨论】:

  • 您也可以使用:set showbreak=\ \ \ \ \ \ \ \ \ \ \ \ \ \ (反斜杠空格组合使分隔字符空间。因此,您可以添加足够的空格以比大多数包装代码更深(例如,10 个空格会更深)比 2 个四个空格制表符和 14 个比 3 个四个空格制表符更深);该空间的一个很好的特点是它在视觉上不会分散注意力(如果这是你想要的)。
  • @Jeromy Anglim 一个小修复:set showbreak=\ \ \ \ \ \ \ \ \ \ \ \ \ \ 的可读性远低于let &amp;showbreak=repeat(' ', 14)
【解决方案4】:

更新: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'
    

    【讨论】:

      【解决方案6】:
      :set smartindent
      :set autoindent
      

      我认为您仍然必须使用 return

      【讨论】:

        【解决方案7】:

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

        :%!xmllint --html --format
        

        【讨论】:

          【解决方案8】:

          宏观解决方案:


          编辑:

          操作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) 加快速度

          【讨论】:

            猜你喜欢
            • 2011-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-03
            • 2011-06-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多