如何在Python中设置vim遇到空行时取消缩进?

3

如果Vim在我键入空行时自动进行取消缩进,将会非常有用,但似乎这不是默认行为。这对Python特别有用,Vim能够配置吗?


1
我以前尝试过这个,发现最终结果非常令人讨厌。想一想:一个平均缩进的代码块大约有3行,所以继续一个代码块的情况比停止一个代码块的情况多出平均3倍。 - orlp
当您再次按Enter键时,Vim会删除空行上的缩进;尝试在没有任何插件的情况下启动vim(vim -u NONE -N),然后逐个加载插件,直到找出是哪个插件导致了这种情况。 - Michael Foukarakis
应该安装Python的缩进文件。在/usr中查找python.vim,或尝试使用替代版本 - Annika Backstrom
2
作为替代方案,在插入模式下使用 <c-d> 可以减少缩进级别。 - Peter Rincker
@Adam Backstrom,已经安装了python.vim,并且对于Python程序缩进工作正常,只是取消缩进无法正常工作。 - Thomson
1个回答

2

我对自己的indent/python.vim进行了一些修改,以在输入第三个空行时启用完全取消缩进。您可以根据自己的需求进行调整。

diff --git a/python.vim b/python.vim
index 0c04e81..c60c30e 100644
--- a/python.vim
+++ b/python.vim
@@ -142,8 +142,14 @@ function GetPythonIndent(lnum)
       " If not, recommend one dedent
       return indent(plnum) - &sw
     endif
-    " Otherwise, trust the user
-    return -1
+
+       " Is user trying to break out of this function?
+       if plnum < a:lnum - 2
+         return 0
+       else
+         " Otherwise, trust the user
+         return -1
+       endif
   endif

   " If the current line begins with a keyword that lines up with "try"
@@ -186,6 +192,11 @@ function GetPythonIndent(lnum)
     return plindent
   endif

+  " Double linebreaks means we're starting a new function (probably)
+  if plnum < a:lnum - 2
+       return 0
+  endif
+
   return -1

 endfunction

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