2

我发现缩进,不管有没有选择,并不总是有效,这似乎是因为这些设置。
据我所知,代码何时有效且缩进正确,但并非无处不在。除了下面的例子,我还没有找到一个模式。

set filetype plugin indent on
set smartindent

以这个 C++ 代码为例:

#include <iostream>
#include <sstream>

int main(void) {
    std::string move;
    int x, y;
    char c;

    while(true) {
        std::cout << "Enter move (x,y): ";
        std::getline(std::cin, move);

        std::stringstream ss(move);
        ss >> x; ss >> c; ss >> y;

        std::cout << "x: " << x << "\n";
        std::cout << "y: " << y << "\n" << std::endl;
    }
}

缩进:

  • 除 之外的任何正确代码#include,例如:
    • <<>>std::getline(std::cin, move);
    • viB<at std::cout << "Enter move (x,y): ";,从而缩进块
  • >>或者>>如果 a#include被错误地缩进
  • 如果#从包含中删除,则使代码不正确,>>有效

不缩进:

  • >>#include <iostream>
  • vip>at :1,从而选择包含并尝试缩进它们

如果缩进失败,文件仍被标记为已更改,即使实际上没有字符被更改。

  1. 这是正确的行为吗?
  2. 为什么 Vim 会区分正确的代码?
  3. 我想保留这个smartindent特性,但即使 Vim 认为它是正确的,我仍然可以手动缩进代码。

如果需要,我的Vim 配置。

4

1 回答 1

2

According to the documentation for smartindent the behavior you are seeing is correct. Below is the relevant part about # indenting

When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. When using the ">>" command, lines starting with '#' are not shifted right.

One way to manually shift it is to change cinkeys. This option defaults to "0{,0},0),:,0#,!^F,o,O,e" where the fifth option is 0#. According to the help form here changes the behavior of # indenting. (NOTE: you need to scroll up a paragraph)

Vim puts a line in column 1 if: - It starts with '#' (preprocessor directives), if 'cinkeys' contains '#'.

To remove this from cinkeys you can put the following in your vimrc

set cinkeys-=0#

You should also probably remove it from indentkeys too.

set indentkeys-=0#
于 2013-05-06T15:37:03.373 回答