gpt4 book ai didi

c++ - Vim 中带有 smartindent 或文件类型缩进的奇怪缩进行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:39 25 4
gpt4 key购买 nike

我发现缩进,有或没有选择,并不总是有效,这似乎是因为这些设置。
据我所知,这是代码有效且正确缩进的时间,但它并没有发生在任何地方。除了下面的示例,我还没有找到模式。

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<std::cout << "Enter move (x,y): "; , 从而缩进 block
  • >>>> 如果 #include缩进错误
  • 如果#从包含中删除,使代码不正确,>> 有效

不缩进:

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

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

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

我的 Vim configuration如果需要的话。

最佳答案

根据 smartindent 的文档您看到的行为是正确的。下面是关于#缩进的相关部分

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.

手动移动它的一种方法是更改​​ cinkeys .此选项默认为 "0{,0},0),:,0#,!^F,o,O,e" 其中第五个选项为 0# .根据帮助表here更改 # 缩进的行为。 (注意:您需要向上滚动一段)

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

要从 cinkeys 中删除它,您可以将以下内容放入您的 vimrc

set cinkeys-=0#

您可能还应该将其从 indentkeys 中删除也是。

set indentkeys-=0#

关于c++ - Vim 中带有 smartindent 或文件类型缩进的奇怪缩进行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16400261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com