vim - 在新的缩进级别 vim 之后的换行符之后缩进

标签 vim

在 vim 中,我只是将选项卡从 2 个空格更改为 4 个空格。这很好用 - 当我点击 Tab 时,它会缩进 4 个空格。当我添加具有相同缩进的新行时,它可以工作。但是 - 当我需要一个新的缩进级别并按回车键时,它只会缩进 2 个空格。

previously fixed code{
    previously indented code;
    if (new amount of indenting should happen){
      only this much is given by vim;
        but hitting tab takes me here (where I want to be);
    }
}

知道我可能需要更改什么设置才能使其正常工作吗?我已经尝试了我能找到的每一个(自动缩进,cindent,设置各种制表位类型变量),但无济于事。

最佳答案

当您通过运行 :help tabstop 检查帮助文件时,你会得到:

'tabstop' 'ts'      number  (default 8)
            local to buffer
    Number of spaces that a <Tab> in the file counts for.  Also see
    |:retab| command, and 'softtabstop' option.

    Note: Setting 'tabstop' to any other value than 8 can make your file
    appear wrong in many places (e.g., when printing it).

    There are four main ways to use tabs in Vim:
    1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
       (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
       will use a mix of tabs and spaces, but typing <Tab> and <BS> will
       behave like a tab appears every 4 (or 3) characters.
    2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
       'expandtab'.  This way you will always insert spaces.  The
       formatting will never be messed up when 'tabstop' is changed.
    3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
       |modeline| to set these values when editing the file again.  Only
       works when using Vim to edit the file.
    4. Always set 'tabstop' and 'shiftwidth' to the same value, and
       'noexpandtab'.  This should then work (for initial indents only)
       for any tabstop setting that people use.  It might be nice to have
       tabs after the first non-blank inserted as spaces if you do this
       though.  Otherwise aligned comments will be wrong when 'tabstop' is
       changed.
所以把它放在你的 .vimrc 文件中,一切正常。
set expandtab       "Use softtabstop spaces instead of tab characters for indentation
set shiftwidth=4    "Indent by 4 spaces when using >>, <<, == etc.
set softtabstop=4   "Indent by 4 spaces when pressing <TAB>

set autoindent      "Keep indentation from previous line
set smartindent     "Automatically inserts indentation in some cases
set cindent         "Like smartindent, but stricter and more customisable

关于vim - 在新的缩进级别 vim 之后的换行符之后缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408178/

相关文章:

vim:如何让 netrw 打开 https 链接?

vim - 删除行中的特定字符

Vim 每个文件映射

vim - 你如何在vim中查找和替换花括号{}?

vim - (g)Vim - 在运行时禁用历史记录/viminfo

vim - 如何使用vim删除每行中的最后一个字符

bash - 在许多文件中用制表符空格替换逗号

vim - 在 Vim 中从光标下的完整路径打开文件

Vim - :set background=<expression>

vim - 在 vimrc 中映射 <esc> 会导致奇怪的箭头行为