为什么VIM不能正确缩进两行for循环?

时间:2013-06-19 01:49:49

标签: c vim editor vi

我在VIM中输入了以下代码,为什么它没有正确缩进?在这里,我想重复相同的循环3次,它们不是嵌套循环。这段代码只是描述我的问题。

#include <stdio.h>

int main() {
  int c;
  for (c = 0; c < 100; ++c)
    printf("%d\t%c\n", c, c);
    for (c = 0; c < 100; ++c)
      printf("%d\t%c\n", c, c);
      for (c = 0; c < 100; ++c)
        printf("%d\t%c\n"; c, c)
  return 0;
}

这是我的.vimrc配置

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent

此设置有什么问题吗?

emacs中的相同代码如下所示

#include <stdio.h>

int main() {
  int c;
  for (c = 0; c < 100; ++c)
    printf("%d\t%c", c, c);
  for (c = 0; c < 100; ++c)
    printf("%d\t%c", c, c);
  for (c = 0; c < 100; ++c)
    printf("%d\t%c", c, c);
  return 0;
}

2 个答案:

答案 0 :(得分:0)

试试这个:

#include <stdio.h>

int main() {
  int c;
  for (c = 0; c < 100; ++c) {
    printf("%d\t%c\n", c, c);
    for (c = 0; c < 100; ++c) {
      printf("%d\t%c\n", c, c);
      for (c = 0; c < 100; ++c) {
        printf("%d\t%c\n"; c, c)
      }
    }
  }
  return 0;
}

答案 1 :(得分:0)

将以下内容添加到vimrc中。这将启用文件类型插件并获得所需的缩进。

 filetype plugin indent on

根据:h smartindent。智能缩进只会插入制表符,除非您将大括号括起来。

Normally 'autoindent' should also be on when using 'smartindent'.
An indent is automatically inserted:
- After a line ending in '{'.
- After a line starting with a keyword from 'cinwords'.
- Before a line starting with '}' (only with the "O" command).
When typing '}' as the first character in a new line, that line is
given the same indent as the matching '{'.