vim:仅显示前导空格/制表符

标签 vim tabs

我想使用 listchars (或某种插件,如果需要的话),使我的缩进在 vim 中可见。但是,我只希望前导空格/制表符可见,而不是所有空格和制表符。

我发现了一些黑客行为here对于空间。评论里还有一个选项here 。问题是,由于选项卡具有可变宽度,因此这些选项不能用于选项卡。最好的情况是,我可以用 >--- 这样的恒定宽度字符串替换制表符,但这意味着如果我有一个 2 个字符的制表符,缩进最终会关闭。

有没有办法只显示前导选项卡,而不显示内联或尾随选项卡?

最佳答案

从 VIM 文档来看,VIM 可以区分前导空格和尾随空格,但不能区分前导制表符或尾随制表符。所以选项卡只是 VIM 的一个选项卡,用 tab:xy 表示。如果您同时定义了 space:ctrail:c,则前者将表示除尾随空格之外的所有空格,而横向表示尾随空格。

                        *'listchars'* *'lcs'*
'listchars' 'lcs'   string  (default "eol:$")
            global
            {not in Vi}
    Strings to use in 'list' mode and for the |:list| command.  It is a
    comma separated list of string settings.
                            *lcs-eol*
      eol:c     Character to show at the end of each line.  When
            omitted, there is no extra character at the end of the
            line.
                            *lcs-tab*
      tab:xy    Two characters to be used to show a tab.  The first
            char is used once.  The second char is repeated to
            fill the space that the tab normally occupies.
            "tab:>-" will show a tab that takes four spaces as
            ">---".  When omitted, a tab is show as ^I.
                            *lcs-space*
      space:c   Character to show for a space.  When omitted, spaces
            are left blank.
                            *lcs-trail*
      trail:c   Character to show for trailing spaces.  When omitted,
            trailing spaces are blank.  Overrides the "space"
            setting for trailing spaces.
                            *lcs-extends*
      extends:c Character to show in the last column, when 'wrap' is
            off and the line continues beyond the right of the
            screen.
                            *lcs-precedes*
      precedes:c    Character to show in the first column, when 'wrap'
            is off and there is text preceding the character
            visible in the first column.
                            *lcs-conceal*
      conceal:c Character to show in place of concealed text, when
            'conceallevel' is set to 1.
                            *lcs-nbsp*
      nbsp:c    Character to show for a non-breakable space character
            (0xA0 (160 decimal) and U+202F).  Left blank when
            omitted.

    The characters ':' and ',' should not be used.  UTF-8 characters can
    be used when 'encoding' is "utf-8", otherwise only printable
    characters are allowed.  All characters must be single width.

    Examples: >
        :set lcs=tab:>-,trail:-
        :set lcs=tab:>-,eol:<,nbsp:%
        :set lcs=extends:>,precedes:<
<   The "NonText" highlighting will be used for "eol", "extends" and
    "precedes".  "SpecialKey" for "nbsp", "space", "tab" and "trail".
    |hl-NonText| |hl-SpecialKey|

更好的方法可能是将 matchsyntax 结合使用,例如::

highlight LeadingSpace ctermbg=red guibg=red
highlight TrailingSpace ctermbg=red guibg=red
highlight LeadingTab ctermbg=red guibg=green
highlight TrailingTab ctermbg=red guibg=green
call matchadd('LeadingSpace', '^\s\+', 80)
call matchadd('TrailingSpace', '\s\+$', 80)
call matchadd('LeadingTab', '^t\+', 99)    
call matchadd('TrailingTab', '\t\+$', 99)       

关于vim:仅显示前导空格/制表符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45018947/

相关文章:

tabs - Dijit TabContainer 小部件图标

javascript - 扩展程序在上传到 chrome 应用商店后不会在新选项卡中打开多个链接

bash - 就地在文件中将时间戳转换为纪元

vim - Vim 文件搜索模式中 * 和 ** 的区别

vim - 有没有比 ctags 效果更好的替代方案?

vim - 动态 vim 表格模式?

vim 更改 :x function to delete buffer instead of save & quit

javascript - 每个选项卡的 session - Google Chrome 扩展

html - 需要帮助为此 HTML 制作标签系统

android - 如何在 ActionBar 中引入 Swipeable Tabs?