如何在Python中遇到空行时将vim设置为unindent?

时间:2011-09-21 13:33:24

标签: python vim

如果我键入一个空行时vim可以自动执行unindentation,这可能很有用,但它似乎不是默认行为。这对Python特别有用,可以配置Vim吗?

1 个答案:

答案 0 :(得分:2)

我为自己的indent/python.vim制作了一些mod,以便在输入第三个空行时启用完整的dedent。您可以根据自己的需要进行调整。

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