Vim Tips Wiki
(New page: While working with python files written by others with varying indentation levels (some using 3 spaces and others using 4 spaces, and some mixing tabs and spaces), it gets tiresome to dete...)
mNo edit summary
Line 1: Line 1:
 
While working with python files written by others with varying indentation levels (some using 3 spaces and others using 4 spaces, and some mixing tabs and spaces), it gets tiresome to determine the right settings and change them manually. While looking for a good script to this automatically, I came across two scripts. The first one on the vim scripts page called DetectIndent, but it didn't work well for some of the python files I tried (some being set to ''shiftwidth'' of 1). The other one I found is a python script called [http://freshmeat.net/projects/vindect/ vindect]. The comment in the file has a suggested setup, but here is what worked better for me:
 
While working with python files written by others with varying indentation levels (some using 3 spaces and others using 4 spaces, and some mixing tabs and spaces), it gets tiresome to determine the right settings and change them manually. While looking for a good script to this automatically, I came across two scripts. The first one on the vim scripts page called DetectIndent, but it didn't work well for some of the python files I tried (some being set to ''shiftwidth'' of 1). The other one I found is a python script called [http://freshmeat.net/projects/vindect/ vindect]. The comment in the file has a suggested setup, but here is what worked better for me:
: Download and extract the python file.
+
* Download and extract the python file.
: Copy it as '''vindent.py''' (without the version number in the filename) to your .vim or vimfiles directory.
+
* Copy it as '''vindent.py''' (without the version number in the filename) to your .vim or vimfiles directory.
: Create a after/ftplugin/python.vim file with the below contents (change '''vimfiles''' to '''.vim''' accordingly):
+
* Create a after/ftplugin/python.vim file with the below contents (change '''vimfiles''' to '''.vim''' accordingly):
 
if !exists('s:configured_vindect')
 
if !exists('s:configured_vindect')
 
if has("python")
 
if has("python")

Revision as of 20:26, 20 March 2009

While working with python files written by others with varying indentation levels (some using 3 spaces and others using 4 spaces, and some mixing tabs and spaces), it gets tiresome to determine the right settings and change them manually. While looking for a good script to this automatically, I came across two scripts. The first one on the vim scripts page called DetectIndent, but it didn't work well for some of the python files I tried (some being set to shiftwidth of 1). The other one I found is a python script called vindect. The comment in the file has a suggested setup, but here is what worked better for me:

  • Download and extract the python file.
  • Copy it as vindent.py (without the version number in the filename) to your .vim or vimfiles directory.
  • Create a after/ftplugin/python.vim file with the below contents (change vimfiles to .vim accordingly):
if !exists('s:configured_vindect')
   if has("python")
      py import sys,os; sys.path.append(os.path.expanduser("~/vimfiles/"))
      try
         py import vindect
         let s:configured_vindect = 1
      catch
         let s:configured_vindect = 0
      endtry
      "if you want different defaults: py vindect.setDefaults(...)
   else
      let s:configured_vindect = 0
   endif
endif

if s:configured_vindect
   py vindect.detect() 
endif