Corrupted VIM Indentation?
Question: From time to time the identation within my python scripts suddenly become, i
suppose you could say corrupted. The identation will suddenly alter, making my
programs fail.

If I look at the file using cat I can see the identation is wrong. But within
VIM it shows as fine. Here is the outputs and settings,

Any ideas ???

**via a 'cat -e'**

    
    
                  validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}',message="Enter a valid hostname.")$
               validate_hostname(host_input)$
       except ValidationError, e:$
                   print type(e)$
           print str(e[0])$
           error = str(e)$
    
    else:$
           error = "Please complete all fields."       $
       $
       print error$
       return [error,host_input,record_input]$
    

**within in VIM** ,

    
    
                       validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}',message="Enter a valid hostname.")
                       validate_hostname(host_input)
               except ValidationError, e:
                   print type(e)
                   print str(e[0])
                   error = str(e)
           else:
               error = "Please complete all fields."
    
           print error
           return [error,host_input,record_input]
    

my **.vimrc** looks like,

    
    
    syntax on
    se bg=dark
    set tabstop=4      " insert 4 spaces when a tab is pressed
    set shiftwidth=4   " change the number of space characters inserted for indentation
    set expandtab      " insert spaces whenver a tab key is pressed
    


Answer: It looks like you have mixed spaces and tabs. The code looks different in
`vim` and in `cat -e` (or simply `less`) because they use different width for
the tab, due to your `set tabstop=4`.

If in `vim` it looks fine, then doing `:retab` should fix it: it will replace
tab characters with the amount of spaces as you see it. The result will look
the same, but all tab characters will be gone.

It's important to have the right `tabstop` value before you do `retab`. For
example if you have the opposite problem--code looks correct in `less` but
broken in `vim`, and you do `:retab` in that state, that will break the Python
script.

Check out this fantastic article about tabs in vim:

<http://vimcasts.org/episodes/tabs-and-spaces/>

In particular, I think you should add these settings to your `.vimrc`:

    
    
    set softtabstop=4
    set smarttab
    

