Thomson
Thomson

Reputation: 21625

How to set vim to unindent when meets a empty line in Python?

It could be useful if vim could do a unindentation automatically when I type an empty line, but it seems it is not the default behavior. This would especially useful for Python, could Vim be configured to do so?

Upvotes: 3

Views: 273

Answers (1)

user7675
user7675

Reputation:

I made some mods to my own indent/python.vim to enable full dedent when the third empty line is entered. You may be able to adapt this to your needs.

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

Upvotes: 2

Related Questions