【问题标题】:Vim: can indentation use spaces after non-whitespace?Vim:缩进可以在非空格之后使用空格吗?
【发布时间】:2014-11-16 12:41:59
【问题描述】:

当光标前面有非空白字符时,是否可以让 VIM 使用空格作为制表符?

例如:

(假设TAB是--->是一个空格。)

--->function(arg);••••••/* comment */
    //            ^ use spaces when pressing TAB after non-whitespace chars.

否则我在按 Tab 时想要真正的标签。

这在使用制表符进行初始缩进时很有用,但所有文本之后的对齐方式都使用空格。

【问题讨论】:

    标签: vim


    【解决方案1】:

    如果没有脚本或插件,这是不可能自动完成的。

    “智能标签”插件专为此任务而设计。 http://www.vim.org/scripts/script.php?script_id=231

    为了减少问题发生的频率,内置的preserveindentcopyindent 选项可防止在设置expandtab 时丢失现有的制表符缩进。

    【讨论】:

      【解决方案2】:

      使用:set expandtab
      这将在插入期间将所有制表符扩展到空格,甚至是行首的制表符(这可能是最好的)。
      使用CTRL-V<Tab> 插入标签。

      来自vim帮助(:help expandtab

      'expandtab' 'et'        boolean (default off)
                              local to buffer
                              {not in Vi}
              In Insert mode: Use the appropriate number of spaces to insert a
              <Tab>.  Spaces are used in indents with the '>' and '<' commands and
              when 'autoindent' is on.  To insert a real tab when 'expandtab' is
              on, use CTRL-V<Tab>.  See also :retab and ins-expandtab.
              NOTE: This option is reset when 'compatible' is set.
      

      使用:set shiftwidth=4(或:set shiftwidth=8)控制每个缩进添加多少空格。

      'shiftwidth' 'sw'       number  (default 8)
                              local to buffer
              Number of spaces to use for each step of (auto)indent.  Used for
              'cindent', >>, <<, etc.
      

      其他感兴趣的设置是softtabstoptabstopautoindent

      【讨论】:

      • 感谢您的回答,回复:even the ones at the beginning of the line (which probably is the best). - 这正是我不想要的。我意识到我的问题的答案很可能是“VIM 无法做到这一点”
      • @ideasman42 键映射 (noremap &lt;F8&gt; :set invexpandtab expandtab?&lt;CR&gt;) 有帮助吗?这将允许您在需要时打开和关闭expandtab。但是,可能会有更好的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 2022-09-28
      相关资源
      最近更新 更多