Vim无法保持缩进

3

Vim在我不写任何东西的情况下保持缩进的行,它们的缩进会被取消...我来举个例子,其中|是插入符。

  1. Caret is indented and I press enter

    if expression:
        print("hello world")
        |
    
  2. Caret is indented on the next line, and now I press the up arrow

    if expression:
        print("hello world")
    
        |
    
  3. Now the caret isn't indented anymore, what happened?

    if expression:
        print("hello world")
    |
    
那么我该如何保留缩进呢?

你可能会对 Vi 和 Vim.SE 感兴趣。 - Bakuriu
如何防止Vim取消缩进空行?在Vi.SE上有相同的问题:https://vi.stackexchange.com/questions/3612/how-do-i-prevent-vim-from-unindenting-empty-lines - user202729
3个回答

4
正如其他人所指出的,如果自动添加缩进,Vim会有意地删除空行的缩进。但是,如果您在该行上插入了任何文本,即使您将其删除,也不会发生这种情况。因此,根据具体情况,如果您想保留空行的缩进,请插入一些文本并使用退格键将其删除。如果您始终想保留缩进,则可以使用映射:
inoremap <CR> <CR>x<BS>

0

这是 Vim 的预期行为。由于您跳过了下一行,它会假定您不想在该特定代码块中编写。


3
那么,我不想让Vim表现出“预期”的行为,我该如何让它不这样做? - Luca

0

在我的安装中,它至少保留了 C 文件的缩进。

您应该尝试在正常模式下使用 o 插入一个新行。

然后根据文档(参见 :help o):

当 'autoindent' 打开时,新行的缩进从上一行获取。当 'smartindent' 或 'cindent' 打开时,C 程序的行缩进会自动调整。

您可能只使用了 'autoindent' 选项。

有关更多信息,请参见 :help indenting

对于非类 C 语言,您可能需要依赖 $VIMRUNTIME/indent 文件。

如果支持您的语言,则可以通过添加 :set filetype indent on 来启用它。否则,您将不得不编写自己的缩进文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接